1

I have a really simple web app. All the important stuff happens in index.py:

from google.appengine.api import users
import webapp2
import os
import jinja2

JINJA_ENVIRONMENT = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
    extensions=['jinja2.ext.autoescape'],
    autoescape=True)

def get_user():
  user = {}
  user['email'] = str(users.get_current_user())
  user['name'], user['domain'] = user['email'].split('@')
  user['logout_link'] = users.create_logout_url('/')
  return user

class BaseHandler(webapp2.RequestHandler):

  def dispatch(self):
    user = get_user()
    template_values = {'user': user}
    if user['domain'] != 'foo.com':
      template_values['page_title'] = 'Access Denied'
      template = '403'
    else:
      template_values['page_title'] = 'Home'
      template = 'index'
    template_engine = JINJA_ENVIRONMENT.get_template('%s.html' % template)
    self.response.write(template_engine.render(template_values))

app = webapp2.WSGIApplication([
    ('/', BaseHandler),
], debug=True)

I'm trying to be a good person and write some local unit tests but - after looking at the documentation - I am totally out of my depth. All I want is a basic framework where I can do something like:

python test_security.py 

and simulate two users hitting the domain - one @foo.com who should get the index template, and one @bar.com who should get the 403 template.

Here's where I've got so far:

import sys

# I don't want to talk about it, let's just ignore this block
sys.path.append('C:\Program Files (x86)\Google\google_appengine\lib\webapp2-2.5.2')
sys.path.append('C:\Program Files (x86)\Google\google_appengine\lib\webob-1.2.3')
sys.path.append('C:\Program Files (x86)\Google\google_appengine\lib\jinja2-2.6')
sys.path.append('C:\Program Files (x86)\Google\google_appengine\lib\yaml-3.10')
sys.path.append('C:\Program Files (x86)\Google\google_appengine\lib\jinja2-2.6')
sys.path.append('C:\Program Files (x86)\Google\google_appengine')
sys.path.append('C:\pytest')


# A few proper imports
import unittest
import webapp2
from google.appengine.ext import testbed

# Import the module I'd like to test
import index

class TestHandlers(unittest.TestCase):
   def test_hello(self):

       self.testbed = testbed.Testbed()
       self.testbed.init_user_stub()
       self.testbed.setup_env(USER_EMAIL='test@foo.com',USER_ID='1', USER_IS_ADMIN='0')

       request = webapp2.Request.blank('/')
       response = request.get_response(main.app)

       print "running test"

       self.assertEqual(response.status_int, 200)
       self.assertEqual(response.body, 'Hello, world!')

Predictably, this doesn't work at all. What am I missing? Am I just wildly overestimating how simple this should be?

mcl
  • 223
  • 2
  • 7
  • What "doesn't work at all" means exactly? – Dan Cornilescu Jun 17 '16 at 11:39
  • "running test" never outputs - so while I think I've defined the test, I'm not sure what I need to do to run it. I'm sure I'll get errors when I try to make it go, but just making it go would be a nice first step :) – mcl Jun 17 '16 at 13:43

2 Answers2

2

If you're planning on invoking this with "python test_security.py", the magic words you are looking for are:

if __name__ == '__main__':
    unittest.main()

This will make your unit test run - at the moment all you're doing is defining it.

Note also that you'll need to change your request.get_response from "main.app" to "index.app".

penitent_tangent
  • 762
  • 2
  • 8
  • 18
1

I suspect (primarily based on the function names) that you should call self.testbed.init_user_stub() before calling self.testbed.setup_env(), not after.

Also you seem to be missing an initial self.testbed = testbed.Testbed() and possibly a testbed.activate() call.

You might want to check out this answer: https://stackoverflow.com/a/21139805/4495081

Community
  • 1
  • 1
Dan Cornilescu
  • 39,470
  • 12
  • 57
  • 97
  • Edited code sample in line with this - thanks. Unfortunately the issue (well, the first of many issues) is that my test module doesn't actually run (the test print in there never fires). I'm surprised (perhaps foolishly) how opaque the world of testing is to a novice. The app works fine but so far the tests are harder than the actual functionality! – mcl Jun 17 '16 at 13:45
  • 1
    It's often hard at first. Don't despair and don't bite more than you can chew. Small steps until you become familiar with the new technology, then it'll be a breeze. Your code doesn't show how/where are you invoking the `test_hello()` method. – Dan Cornilescu Jun 17 '16 at 14:16
  • Thanks! I'll keep trying :) – mcl Jun 17 '16 at 14:43