def process_code(): code = request.args.get('code', '') auth = AuthService(code) auth_res = auth.auth() if not auth_res: abort(401) return redirect(url_for('player.show'))
class AuthServiceTest(unittest.TestCase): def setUp(self): test = self class TestWebClient(object): def receive_tokens(self, code): return dict(access_token='test_access_token') def user_info(self, access_token): return dict(id=test.user_id, username=test.username) self.webclient_mock = TestWebClient() self.database = connection() self.service = AuthService('some_auth_code') class TestConf(AppConfig): TESTING = True self.app = create_app(TestConf) def tearDown(self): self.database.accounts.remove(ObjectId(self.user_id)) def test_auth_new_user(self): self.username = '******'.format(repr(self)) test_user = dict(name=self.username, chances=0) self.user_id = str(ObjectId()) with self.app.test_request_context('/'): with self._patch_webclient(): result = self.service.auth() self.assertTrue(result) def test_auth_existent_user(self): self.username = '******'.format(repr(self)) test_user = dict(name=self.username, chances=222) self.user_id = str(self.database.accounts.insert(test_user)) with self.app.test_request_context('/'): with self._patch_webclient(): result = self.service.auth() self.assertTrue(result) def test_auth_existent_user_and_update_name(self): self.username = '******'.format(repr(self)) test_user = dict(name=self.username, chances=222) self.user_id = str(self.database.accounts.insert(test_user)) self.username = self.username + ' updated' with self.app.test_request_context('/'): with self._patch_webclient(): self.service.auth() user = users.current() self.assertEqual(user.name, self.username) def _patch_webclient(self): return patch.object(AuthService, 'webclient', self.webclient_mock)
def setUp(self): test = self class TestWebClient(object): def receive_tokens(self, code): return dict(access_token='test_access_token') def user_info(self, access_token): return dict(id=test.user_id, username=test.username) self.webclient_mock = TestWebClient() self.database = connection() self.service = AuthService('some_auth_code') class TestConf(AppConfig): TESTING = True self.app = create_app(TestConf)