def do_business(self, stop_on_error=True): if self._google_user_future: g_user_key = self._google_user_future.get_result() if g_user_key: self.result = DestinationsSearch(ExternalToMainUser, g_user_key).execute().result[0] else: g_user = self.google_user self._save_google_user_future = GoogleUser(google_id=g_user.user_id()).put_async() self.result = MainUser(name=g_user.nickname(), email=g_user.email()) self._save_main_user = self.result.put_async()
def test_logged_user(self, facade): user = MainUser() user.put() cmd_mock = Mock() cmd_mock.execute = Mock(return_value=cmd_mock) cmd_mock.result = user facade.logged_user = Mock(return_value=cmd_mock) dependency = {} middleware.LoggedUserMiddleware(Mock(), dependency, Mock()).set_up() self.assertEqual({'_login_path': '/login', '_logout_path': '/logout', '_logged_user': user}, dependency)
def test_google_user_logged_for_the_second_time(self): google_account_user = Mock() google_account_user.user_id = lambda: '123' g_key = GoogleUser(google_id='123').put() main_user = MainUser(name='foo', email='*****@*****.**') main_user.put() ExternalToMainUser(origin=g_key, destination=main_user.key.id()).put() user = facade.login_google(google_account_user, Mock()).execute().result self.assertEqual('*****@*****.**', user.email) self.assertEqual('foo', user.name) self.assertEqual(main_user, user) self.assertEqual(1, len(GoogleUser.query().fetch())) self.assertEqual(1, len(MainUser.query().fetch()))
def do_business(self): super(PasswordlessDetailCheck, self).do_business() if not self.errors: app = self.result self.result = None fetch_cmd = UrlFetchCommand(self.detail_url, {'ticket': self.ticket, 'app_id': app.app_id, 'token': app.token}, 'POST').execute() self.errors.update(fetch_cmd.errors) if not self.errors: dct = json.loads(fetch_cmd.result.content) cmd_parallel = CommandParallel(GetMainUserByEmail(dct['email']), GetPasswordlessUser(dct['id'])) cmd_parallel.execute() main_user, passwordless_user = cmd_parallel[0].result, cmd_parallel[1].result if passwordless_user: main_user = MainUserSearch(passwordless_user).execute().result elif main_user: passwordless_user_key = PasswordlessUser(external_id=dct['id']).put() self._to_commit = ExternalToMainUser(origin=passwordless_user_key, destination=main_user.key) else: main_user = MainUser(name=dct['email'], email=dct['email']) passwordless_user = PasswordlessUser(external_id=dct['id']) ndb.put_multi([main_user, passwordless_user]) self._to_commit = ExternalToMainUser(origin=passwordless_user.key, destination=main_user.key) self.result = main_user
def test_query_email_starts_with(self): users_ordered_by_unicode = [mommy.save_one(MainUser, email='*****@*****.**' % b) for b in 'a b c'.split(' ')] db_users = MainUser.query_email_starts_with().fetch() self.assertListEqual(users_ordered_by_unicode, db_users) db_users = MainUser.query_email_starts_with('a').fetch() self.assertListEqual(users_ordered_by_unicode[:1], db_users) db_users = MainUser.query_email_starts_with('aa@').fetch() self.assertListEqual([], db_users) abc_user = mommy.save_one(MainUser, email='*****@*****.**') db_users = MainUser.query_email_starts_with('aa').fetch() self.assertListEqual([], db_users) db_users = MainUser.query_email_starts_with('ab').fetch() self.assertListEqual([abc_user], db_users)
class GoogleLogin(Command): def __init__(self, google_user, response, user_cookie): super(GoogleLogin, self).__init__() self.google_user = google_user self.user_cookie = user_cookie self.response = response self._google_user_future = None self._save_google_user_future = None def set_up(self): if self.google_user: query = GoogleUser.query_by_google_id(self.google_user.user_id()) self._google_user_future = query.get_async(keys_only=True) def do_business(self, stop_on_error=True): if self._google_user_future: g_user_key = self._google_user_future.get_result() if g_user_key: self.result = DestinationsSearch(ExternalToMainUser, g_user_key).execute().result[0] else: g_user = self.google_user self._save_google_user_future = GoogleUser(google_id=g_user.user_id()).put_async() self.result = MainUser(name=g_user.nickname(), email=g_user.email()) self._save_main_user = self.result.put_async() def commit(self): if self._save_google_user_future: google_user_key = self._save_google_user_future.get_result() main_user_key = self._save_main_user.get_result() facade.write_cookie(self.response, self.user_cookie, {'id': self.result.key.id()}).execute() return ExternalToMainUser(origin=google_user_key, destination=main_user_key) if self.result: facade.write_cookie(self.response, self.user_cookie, {'id': self.result.key.id()}).execute()
def test_google_user_logged_for_the_second_time(self, log_main_user_in): google_account_user = self.mock_google_account_user() g_key = GoogleUser(external_id='123').put() main_user = MainUser(name='foo', email='*****@*****.**') main_user.put() ExternalToMainUser(origin=g_key, destination=main_user.key.id()).put() response = Mock() cmd = facade.login_google(google_account_user, response).execute() self.assertTrue(cmd.result) user = cmd.main_user_from_external self.assertEqual('*****@*****.**', user.email) self.assertEqual('foo', user.name) self.assertEqual(main_user, user) self.assertEqual(1, len(GoogleUser.query().fetch())) self.assertEqual(1, len(MainUser.query().fetch())) self.assertIsNone(cmd.pending_link) log_main_user_in.assert_called_once_with(user, response, 'userck')
def find_users_by_email_starting_with(email_prefix=None, cursor=None, page_size=30): """ Returns a command that retrieves users by its email_prefix, ordered by email. It returns a max number of users defined by page_size arg. Next result can be retrieved using cursor, in a next call. It is provided in cursor attribute from command. """ email_prefix = email_prefix or '' return ModelSearchCommand(MainUser.query_email_starts_with(email_prefix), page_size, cursor, cache_begin=None)
class ResultTests(gae.GAETestCase): def setUp(self): super(ResultTests, self).setUp() self.user = MainUser(name='vovo', email='*****@*****.**') self.user.put() self.game = Game(tit='teste', map='brasil') self.game.put() def test_create_new_result(self): result = Result.change_result_attrs(points=1, won_medal=True, game_title=self.game.tit, game=self.game, user_key=self.user.key, duration=0.20) result.put() self.assertEquals(result.last_points, 1) self.assertEquals(result.game_title, self.game.tit) self.assertEquals(result.won_medal, True) self.assertEquals(result.game, self.game.key) self.assertEquals(result.last_duration, 0.20) self.assertEquals(result.best_duration, 0.20) def test_update_result(self): result = Result.change_result_attrs(points=1, won_medal=True, game_title=self.game.tit, game=self.game, user_key=self.user.key, duration=0.20) result.put() self.assertEquals(result.best_points, 1) self.assertEquals(result.best_duration, 0.20) self.assertEquals(result.best_date, date.today()) self.assertEquals(result.frequency, 1) result = Result.change_result_attrs(points=2, duration=0.10, won_medal=True, game_title=self.game.tit, game=self.game, user_key=self.user.key) self.assertEquals(result.best_points, 2) self.assertEquals(result.best_duration, 0.10) self.assertEquals(result.best_date, date.today()) self.assertEquals(result.frequency, 2)
def find_users_by_email_and_group(email_prefix=None, group=None, cursor=None, page_size=30): """ Returns a command that retrieves users by its email_prefix, ordered by email and by Group. If Group is None, only users without any group are going to be searched It returns a max number of users defined by page_size arg. Next result can be retrieved using cursor, in a next call. It is provided in cursor attribute from command. """ email_prefix = email_prefix or '' return ModelSearchCommand(MainUser.query_email_and_group(email_prefix, group), page_size, cursor, cache_begin=None)
def test_google_user_logged_for_the_first_time(self): google_account_user = Mock() google_account_user.user_id = lambda: '123' google_account_user.email = lambda: '*****@*****.**' google_account_user.nickname = lambda: 'foo' user = facade.login_google(google_account_user, Mock()).execute().result self.assertEqual('*****@*****.**', user.email) self.assertEqual('foo', user.name) self.assertEqual(MainUser.query().get(), user) google_user = OriginsSearch(ExternalToMainUser, user).execute().result[0] self.assertEqual('123', google_user.google_id)
def index(_resp, id=None, context={}): if id: game = Game.get_by_id(int(id)) if not game: return RedirectResponse(router.to_path(index)) game_form = GameFormTable() game_form.fill_with_model(game) query = ArcGen.query(ArcGen.destination == game.key) arc = query.fetch() query = MainUser.query(MainUser.key == arc[0].origin) autor = query.fetch() context = {'properties': game_form, 'edit': True, 'id' : id, 'autor' : autor[0]} return TemplateResponse(context, template_path="/admin/games-management-form.html") return TemplateResponse(context, template_path="admin/games-management-form.html")
def index(_resp, id=None, context={}): if id: game = Game.get_by_id(int(id)) if not game: return RedirectResponse(router.to_path(index)) game_form = GameFormTable() game_form.fill_with_model(game) query = ArcGen.query(ArcGen.destination == game.key) arc = query.fetch() query = MainUser.query(MainUser.key == arc[0].origin) autor = query.fetch() context = {'properties': game_form, 'edit': True, 'id' : id, 'autor' : autor[0]} return TemplateResponse(context, template_path="/admin/games-management-form-dialog.html") return TemplateResponse(context, template_path="admin/games-management-form-dialog.html")
def test_facebook_user_logged_for_the_second_time(self, log_main_user_in): f_key = FacebookUser(external_id='123').put() main_user = MainUser(name='foo', email='*****@*****.**') main_user.put() ExternalToMainUser(origin=f_key, destination=main_user.key.id()).put() response = Mock() fetch_mock = self.setup_fetch_mock() cmd = facade.login_facebook('', response) cmd._fetch_facebook = fetch_mock cmd.execute() self.assertTrue(cmd.result) user = cmd.main_user_from_external self.assertEqual('*****@*****.**', user.email) self.assertEqual('foo', user.name) self.assertEqual(main_user, user) self.assertEqual(1, len(FacebookUser.query().fetch())) self.assertEqual(1, len(MainUser.query().fetch())) self.assertIsNone(cmd.pending_link) log_main_user_in.assert_called_once_with(user, response, 'userck')
def tasks(cursor=None): cmd = UserSearch(cursor) user = cmd() if user: user = user[0] email = SingleEmailUserSearch(user)().email MainUser(name=email, email=email, key=to_node_key(user), creation=user.creation).put() path = to_path(tasks, cmd.cursor.urlsafe()) taskqueue.add(url=path) else: send_mail_to_admins(settings.SENDER_EMAIL, 'Migration End', 'Migration end')
def test_google_user_logged_for_the_first_time_with_no_conflict(self, log_main_user_in): google_account_user = self.mock_google_account_user() response = Mock() cmd = facade.login_google(google_account_user, response).execute() self.assertTrue(cmd.result) user = cmd.main_user_from_external self.assertEqual('*****@*****.**', user.email) self.assertEqual('foo', user.name) self.assertEqual(MainUser.query().get(), user) google_user = ExternalUserSearch(user).execute().result[0] self.assertEqual('123', google_user.external_id) log_main_user_in.assert_called_once_with(user, response, 'userck') self.assertIsNone(cmd.pending_link)
def test_google_user_logged_for_the_first_time_with_conflict(self, log_main_user_in): google_account_user = self.mock_google_account_user() main_user = mommy.save_one(MainUser, email='*****@*****.**') cmd = facade.login_google(google_account_user, Mock()).execute() self.assertFalse(cmd.result) self.assertIsNone(cmd.main_user_from_external) self.assertEqual(1, len(GoogleUser.query().fetch())) self.assertEqual(1, len(MainUser.query().fetch())) self.assertIsNotNone(cmd.pending_link) self.assertEqual(PendingExternalToMainUser.query().get(), cmd.pending_link) self.assertEqual(cmd.external_user.key, cmd.pending_link.external_user) self.assertEqual(main_user.key, cmd.pending_link.main_user) self.assertEqual('123', cmd.external_user.external_id) self.assertFalse(log_main_user_in.called)
def find_users_by_email_and_group(email_prefix=None, group=None, cursor=None, page_size=30): """ Returns a command that retrieves users by its email_prefix, ordered by email and by Group. If Group is None, only users without any group are going to be searched It returns a max number of users defined by page_size arg. Next result can be retrieved using cursor, in a next call. It is provided in cursor attribute from command. """ email_prefix = email_prefix or '' return ModelSearchCommand(MainUser.query_email_and_group( email_prefix, group), page_size, cursor, cache_begin=None)
def test_facebook_user_logged_for_the_first_time_with_no_conflict(self, log_main_user_in): response = Mock() fetch_mock = self.setup_fetch_mock() cmd = facade.login_facebook('', response) cmd._fetch_facebook = fetch_mock cmd.execute() self.assertTrue(cmd.result) user = cmd.main_user_from_external self.assertEqual('*****@*****.**', user.email) self.assertEqual('foo', user.name) self.assertEqual(MainUser.query().get(), user) facebook_user = ExternalUserSearch(user).execute().result[0] self.assertEqual('123', facebook_user.external_id) log_main_user_in.assert_called_once_with(user, response, 'userck') self.assertIsNone(cmd.pending_link)
def test_facebook_user_logged_for_the_first_time_with_conflict(self, log_main_user_in): main_user = mommy.save_one(MainUser, email='*****@*****.**') response = Mock() fetch_mock = self.setup_fetch_mock() cmd = facade.login_facebook('', response) cmd._fetch_facebook = fetch_mock cmd.execute() self.assertFalse(cmd.result) self.assertIsNone(cmd.main_user_from_external) self.assertEqual(1, len(FacebookUser.query().fetch())) self.assertEqual(1, len(MainUser.query().fetch())) self.assertIsNotNone(cmd.pending_link) self.assertEqual(PendingExternalToMainUser.query().get(), cmd.pending_link) self.assertEqual(cmd.external_user.key, cmd.pending_link.external_user) self.assertEqual(main_user.key, cmd.pending_link.main_user) self.assertEqual('123', cmd.external_user.external_id) self.assertFalse(log_main_user_in.called)
def do_business(self): super(Login, self).do_business() # if no conflict if self.result: if self.external_user is None and self.main_user_from_external is None: self.external_user = self.external_user_class( external_id=self.external_id) self.main_user_from_external = MainUser(name=self.user_name, email=self.email) external_user_key, main_user_key = ndb.put_multi( [self.external_user, self.main_user_from_external]) self._arc = ExternalToMainUser(origin=external_user_key, destination=main_user_key) log_main_user_in(self.main_user_from_external, self.response, self.user_cookie) else: if self.external_user is None: self.external_user = self.external_user_class( external_id=self.external_id) self.external_user.put() self.pending_link = PendingExternalToMainUser( external_user=self.external_user.key, main_user=self.main_user_from_email.key)
def update(user_id, groups): facade.update_user_groups(user_id, groups).execute() save_user_doc(MainUser.get_by_id(int(user_id)))
def __init__(self, email): super(GetMainUserByEmail, self).__init__(MainUser.query_email(email), 1)
def test_save_when_previous_command_does_not_find_user(self): cmd = CommandSequential(facade.get_user_by_email('*****@*****.**'), facade.save_user_cmd('*****@*****.**')) user = cmd() self.assertIsNotNone(user) self.assertEqual(user, MainUser.query_email('*****@*****.**').get()) self.assertEqual(1, len(MainUser.query().fetch()))
def test_succes(self): saved_user = facade.save_user_cmd('*****@*****.**', 'foo', groups=['ADMIN'])() self.assertEqual(MainUser.query_email('*****@*****.**').get(), saved_user)
def setUp(self): super(ResultTests, self).setUp() self.user = MainUser(name='vovo', email='*****@*****.**') self.user.put() self.game = Game(tit='teste', map='brasil') self.game.put()