def get(self, id): service = UserService() self.set_header('Content-Type', 'application/json') try: user = yield service.get(id) self.write({'status': 'ok', 'user': user.to_dict()}) except ResourceNotFoundError: self.write({"status": "failed", "errorMessage": "Not found."})
def get(self): user_entity = User(user_name='fg', password='******', email='fgdsb@fgdsb') service = UserService() try: user = yield service.create_with_entity(user_entity) self.write('Added {}'.format(user.uuid)) except EntityAlreadyExistsError: self.write('User name {} exist.'.format(user_entity.user_name))
def get(self, user_uuid): service = UserService() self.set_header('Content-Type', 'application/json') try: stars = yield service.get_all_liked_stars(user_uuid) self.write({ "status": "ok", "stars": [star.to_dict() for star in stars] }) except ResourceNotFoundError: self.write({"status": "failed", "errorMessage": "User not found."})
def test_like_star(self): user = yield UserService().create_with_entity( self.fixture_with_new_uuid('user')) star_uuid = '24c8cf4b-b551-4bef-95fe-30e2a6749929' self.assertTrue(user.liked_stars[star_uuid]) yield UserService().like_star( user.uuid, star_uuid, False, ) user = yield UserService().get(user.uuid) self.assertFalse(user.liked_stars[star_uuid])
def test_like_title(self): user = yield UserService().create_with_entity( self.fixture_with_new_uuid('user')) title_uuid = '210eb8b3-9b82-4762-add9-0727dc2bcc99' self.assertTrue(user.liked_titles[title_uuid]) yield UserService().like_title( user.uuid, title_uuid, False, ) user = yield UserService().get(user.uuid) self.assertFalse(user.liked_titles[title_uuid])
def get(self, user_uuid, star_uuid): service = UserService() self.set_header('Content-Type', 'application/json') try: if self.get_argument("like", 'n', True) == 'y': like = True else: like = False yield service.like_star(user_uuid, star_uuid, like) self.write({"status": "ok"}) except ResourceNotFoundError: self.write({"status": "failed", "errorMessage": "User not found."})
def test_user_dislike_title(http_client, base_url): user_entity = User(user_name='fg_dislike_video', password='******', email='fgdsb@fgdsb') user = yield UserService().create_with_entity(user_entity) title_entity = Title( title_id='dislike', title='test title 1', video_path='test', file_names=['test file'], description='test des', stars=[str(uuid.uuid4())], video_size=1000000000, rate=8, ) title = yield TitleService().create_with_entity(title_entity) mock_service = gen_mock_service() expect(mock_service).like_title.with_args(user.uuid, title.uuid, False).and_return_future(None) response = yield http_client.fetch(base_url + '/api/users/' + user.uuid + '/like_titles/' + title.uuid + '?like=n') assert response.body == '{"status": "ok"}' assert response.code == httplib.OK
def post(self): data = json.loads(self.request.body) user_name = data.get('username') password = data.get('password') log_dict = { 'request': data, 'route': 'auth/login', } user = yield UserService().get_by_name(user_name) respond = {'result': 'Success'} if not user or user.password != password: self.set_status(403) respond['result'] = 'Failed' respond['reason'] = 'User not found' if not user else 'Password not match' else: token = jwt.encode({ 'userName': user_name, 'uuid': user.uuid, }, 'secret', algorithm='HS256') respond['token'] = token log_dict.update({'respond': respond}) logger.info(log_dict) self.write(respond)
def test_check_duplicates(self, mock_get): fake_user = mock.Mock(user_name='fgdsb') mock_get.return_value = gen.maybe_future(fake_user) dup = yield UserService().check_duplicates(fake_user) mock_get.assert_called_once_with('fgdsb') self.assertTrue(dup)
def test_get_all_liked_stars_user_not_found(self, mock_get): mock_get.return_value = gen.maybe_future(None) fake_uuid = 'c736b780-11b6-4190-8529-4d89504b76a0' with self.assertRaises(ResourceNotFoundError): yield UserService().get_all_liked_stars(fake_uuid) mock_get.assert_called_once_with(fake_uuid)
def test_get_by_name(self, mock_get): fake_user = mock.Mock() mock_get.return_value = gen.maybe_future(fake_user) fake_user_name = 'fg' user = yield UserService().get_by_name(fake_user_name) mock_get.assert_called_once_with(fake_user_name) self.assertEquals(user, fake_user)
def post(self): user_name = self.get_body_argument('user_name') password = self.get_body_argument('password') email = self.get_body_argument('email') user = User(user_name=user_name, password=password, email=email) service = UserService() self.set_header('Content-Type', 'application/json') try: user = yield service.create_with_entity(user) self.write({"status": "ok", "uuid": user.uuid}) except EntityAlreadyExistsError: self.write({ "status": "failed", "errorMessage": "User name {} exist.".format(user_name) })
def get(self): service = UserService() self.set_header('Content-Type', 'application/json') if not self.get_argument("user_name", None, True): users = yield service.get_all() self.write({ "status": "ok", "users": [user.to_dict() for user in users] }) else: user_name = self.get_argument("user_name", None, True) user = yield service.get_by_name(urllib.unquote(user_name)) if not user: self.write({"status": "failed", "errorMessage": "Not found."}) else: self.write({'status': 'ok', 'user': user.to_dict()})
def test_get_no_liked_titles(self, mock_get_all_by_uuids, mock_get): fake_uuid = 'c736b780-11b6-4190-8529-4d89504b76a0' fake_user = User(liked_titles={}) fake_titles = [] mock_get.return_value = gen.maybe_future(fake_user) mock_get_all_by_uuids.return_value = gen.maybe_future(fake_titles) titles = yield UserService().get_all_liked_titles(fake_uuid) mock_get.assert_called_once_with(fake_uuid) mock_get_all_by_uuids.assert_called_once_with([]) self.assertEquals(titles, fake_titles)
def test_get_user(http_client, base_url): user_entity = User(user_name='fg', password='******', email='fgdsb@fgdsb') user = yield UserService().create_with_entity(user_entity) mock_service = gen_mock_service() expect(mock_service).get.and_return_future(user) response = yield http_client.fetch(base_url + '/api/users/' + user.uuid) result_object = json.loads(response.body) assert result_object['status'] == 'ok' assert result_object['user'] == user.to_dict() assert response.code == httplib.OK
def test_user_like_stars(http_client, base_url): user_entity = User(user_name='fg_like_stars', password='******', email='fgdsb@fgdsb') user = yield UserService().create_with_entity(user_entity) mock_service = gen_mock_service() expect(mock_service).get_all_liked_stars.and_return_future([]) response = yield http_client.fetch(base_url + '/api/users/' + user.uuid + '/like_stars') assert response.body == '{"status": "ok", "stars": []}' assert response.code == httplib.OK
def test_get_all_liked_titles(self, mock_get_all_by_uuids, mock_get): fake_uuid = 'c736b780-11b6-4190-8529-4d89504b76a0' fake_user = User( liked_titles={'210eb8b3-9b82-4762-add9-0727dc2bcc99': True}) fake_titles = mock.Mock() mock_get.return_value = gen.maybe_future(fake_user) mock_get_all_by_uuids.return_value = gen.maybe_future(fake_titles) titles = yield UserService().get_all_liked_titles(fake_uuid) mock_get.assert_called_once_with(fake_uuid) mock_get_all_by_uuids.assert_called_once_with( ['210eb8b3-9b82-4762-add9-0727dc2bcc99']) self.assertEquals(titles, fake_titles)
def test_get_user_by_name(http_client, base_url): user_entity = User( user_name='fgbyname', password='******', email='fgdsb@fgdsb' ) user = yield UserService().create_with_entity(user_entity) mock_service = gen_mock_service() expect(mock_service).get_by_name.with_args(user.user_name).and_return_future(user) response = yield http_client.fetch(base_url + '/api/users?user_name=' + user.user_name) assert json.loads(response.body) == {"status": "ok", "user": user.to_dict()} assert response.code == httplib.OK
def test_user_dislike_star(http_client, base_url): user_entity = User(user_name='fg_dislike_star', password='******', email='fgdsb@fgdsb') user = yield UserService().create_with_entity(user_entity) star_entity = Star(name='fgdislike', hiragana='fgdsb', english_id='fgdsb') star = yield StarService().create_with_entity(star_entity) mock_service = gen_mock_service() expect(mock_service).like_star.with_args(user.uuid, star.uuid, False).and_return_future(None) response = yield http_client.fetch(base_url + '/api/users/' + user.uuid + '/like_stars/' + star.uuid + '?like=n') assert response.body == '{"status": "ok"}' assert response.code == httplib.OK
def get_user_from_cookie(self): user_cookie_dict = self.get_user_cookie() if user_cookie_dict is None: raise Return((None, None)) user_uuid = user_cookie_dict.get(COOKIE_KEY_USER_UUID, None) if not user_uuid: logger.warning('No UUID found in the user cookie') self.clear_cookie(USER_COOKIE_NAME) raise Return((None, None)) user = yield UserService().get(user_uuid) if user is None: logger.warning('User not found') self.clear_cookie(USER_COOKIE_NAME) raise Return((None, None)) raise Return((user_cookie_dict, user))
def gen_mock_service(): class_name = 'bootcamp.handlers.add_user.UserService' mock_service = UserService() service_class = patch_class(class_name) allow_constructor(service_class).and_return(mock_service) return mock_service