def setUp(self): super(TestCaseHistory, self).setUp() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() app = endpoints.api_server([ UserRollHistoryHandler, UserGamesHistoryHandler, UserGamesHandler ], restricted=False) self.testapp = webtest.TestApp(app) self.user_one = User(username='******', email='*****@*****.**') self.user_two = User(username='******', email='*****@*****.**') self.user_one.put() self.user_two.put() self.jwt_token_player_one = token.encode_jwt({"user_key": self.user_one.key.urlsafe()}) self.jwt_token_player_two = token.encode_jwt({"user_key": self.user_two.key.urlsafe()}) self.game = Game(player_one=self.user_one.key, player_one_name=self.user_one.username, player_two=self.user_two.key, player_two_name=self.user_two.username) self.game.put() self.user_one_turncard = TurnCard(owner=self.user_one.key, game=self.game.key) self.user_two_turncard = TurnCard(owner=self.user_two.key, game=self.game.key) self.user_one_turncard.put() self.user_two_turncard.put()
def setUp(self): super(TestCaseAuth, self).setUp() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() app = endpoints.api_server([ ReauthHandler ], restricted=False) self.testapp = webtest.TestApp(app) user_one = User(username='******', email='*****@*****.**').put() user_two = User(username='******', email='*****@*****.**').put() self.jwt_token_player_one = token.encode_jwt({"user_key": user_one.urlsafe()}) self.jwt_token_player_two = token.encode_jwt({"user_key": user_two.urlsafe()})
def setUp(self): super(TestCaseInvites, self).setUp() self.testbed.init_datastore_v3_stub() self.testbed.init_memcache_stub() app = endpoints.api_server( [CreateInviteHandler, RetrieveInviteHandler, CancelInviteHandler], restricted=False) self.testapp = webtest.TestApp(app) self.user_one = User(username='******', email='*****@*****.**') self.user_two = User(username='******', email='*****@*****.**') self.user_one.put() self.user_two.put() self.jwt_token_player_one = token.encode_jwt( {"user_key": self.user_one.key.urlsafe()}) self.jwt_token_player_two = token.encode_jwt( {"user_key": self.user_two.key.urlsafe()})
def test_retrieve_game_history(self): users = [] for _ in xrange(0, 10): users.append( User(username='******' % (_ + 1), email='*****@*****.**', wins=random.randint(0, 10)).put()) # loop through and make some games user_one = users[0].get() for _ in xrange(0, 5): user_two = users[9 - _].get() game = Game(player_one=user_one.key, player_one_name=user_one.username, player_two=user_two.key, player_two_name=user_two.username, player_one_upper_sub_total=random.randint(10, 80), player_one_lower_total=random.randint(30, 150), player_two_upper_sub_total=random.randint(10, 80), player_two_lower_total=random.randint(30, 150), player_one_completed=True, player_two_completed=False if _ == 4 else True) game.put() resp = self.testapp.post_json('/_ah/spi/UserGamesHistoryHandler.games_history', { "jwt_token": token.encode_jwt({"user_key": user_one.key.urlsafe()}) }) resp = json.loads(resp.body) self.assertEqual(len(resp['games']), 4, '4 historical games were not retrieved')
def test_retrieve_game_moves(self): users = [] for _ in xrange(0, 2): users.append( User(username='******' % (_ + 1), email='*****@*****.**', wins=random.randint(0, 10)).put()) user_one = users[0].get() user_two = users[1].get() game = Game(player_one=user_one.key, player_one_name=user_one.username, player_two=user_two.key, player_two_name=user_two.username) game.put() user_one_turncard = TurnCard( owner=user_one.key, game=game.key ) user_one_turncard.put() # let's take the turns for _ in xrange(0, 15): roll = [] turn = Turn() for _ in xrange(0, 5): roll.append(random.randint(0, 6)) turn.roll_one = roll roll = [] for _ in xrange(0, 5): roll.append(random.randint(0, 6)) turn.roll_two = roll roll[0] = random.randint(0, 6) turn.roll_three = roll turn.put() user_one_turncard.turns.append(turn.key) user_one_turncard.put() resp = self.testapp.post_json('/_ah/spi/UserRollHistoryHandler.roll_history', { "jwt_token": token.encode_jwt({"user_key": user_one.key.urlsafe()}), "game_key": game.key.urlsafe() }) resp = json.loads(resp.body) self.assertEqual(len(resp['rolls']), 15, '15 historical turns were not retrieved')
def test_cancel_invite_not_exist(self): # try to cancel an invite that doesn't belong to the user user_three = User(username='******', email='*****@*****.**') user_three.put() user_three_jwt = token.encode_jwt( {"user_key": user_three.key.urlsafe()}) invite = Invite(to_player=self.user_one.key, to_player_name=self.user_one.username, from_player=self.user_two.key, from_player_name=self.user_two.username).put() resp = self.testapp.post('/_ah/spi/CancelInviteHandler.cancel_invite', params=json.dumps({ "jwt_token": user_three_jwt, "target_user": self.user_one.key.urlsafe() }), content_type='application/json', expect_errors=True) self.assertIn('400', str(resp))
def auth_user(self, request): """ Verifies the identity of the user via Oauth using the user's auth code. If the user already exists, we return a JWT with their claim. If they don't already exist, we create their user in the datastore and return a JWT with their claim. """ # we are using this application to make the request on behalf of the user client_id = os.environ.get('CLIENT_ID') client_secret = os.environ.get('CLIENT_SECRET') # code associated with the user so we can verify their identity auth_code = request.auth_code if client_secret is None: raise endpoints.InternalServerErrorException( 'This application has not been configured with proper credentials' ) # make sure we actually have the keys if auth_code.replace(' ', '') == '': raise endpoints.BadRequestException('Auth code not provided') # verifying the user try: # we have the credentials associated with this client so # let's go ahead and verify the user using OAuth credentials = client.credentials_from_code(client_id, client_secret, 'email', auth_code) http_auth = credentials.authorize(httplib2.Http()) service = discovery.build('oauth2', 'v2', http=http_auth) userinfo = service.userinfo().get().execute( ) # we should have the userinfo except: # were we given the wrong access token? raise endpoints.BadRequestException( 'Unable to authenticate access token. Verify the client ID is correct' ) # ok now create the user and jwt and pass it back! email = userinfo.get('email') if email is None: raise endpoints.BadRequestException( 'Unable to fetch email for this user') # check whether the user exists user = User.get_by_id(email) if user is None: try: # create the user username = email.split('@')[0] user = User(username=username, email=email) user.key = Key('User', email) user.put() except Exception as e: # print e.message raise endpoints.InternalServerErrorException( 'An error occurred while attempting to create user') # create the JWT and send it back to the user. This should # be used on all subsequent requests! payload = {"user_key": user.key.urlsafe()} try: jwt = token.encode_jwt(payload) except LookupError: raise endpoints.InternalServerErrorException( 'An error occurred while attempting to create credentials') return UserAuthFormResponse(jwt_token=jwt)