def test_user_quest_can_update_attributes(self): user = User(username='******', email='*****@*****.**', xp=0) user.insert() quest_1 = Quest(name="Make'a da pancake!", xp=5, level=1, encounter_req=3, type='active') db.session.add(quest_1) db.session.commit() user_quest_1 = UserQuest(quest_id=quest_1.id, user_id=user.id, progress=1, completion_status=False) user_quest_1.insert() self.assertEqual(1, user_quest_1.progress) user_quest_1 = UserQuest(quest_id=quest_1.id, user_id=user.id, progress=2, completion_status=False) user_quest_1.update() self.assertEqual(2, user_quest_1.progress)
class GetUserTest(unittest.TestCase): def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client() self.user_1 = User(username='******', email='e1@', xp=0) self.user_1.insert() self.payload = {'email': 'e1@'} def tearDown(self): db.session.remove() db_drop_everything(db) self.app_context.pop() def test_happypath_get_a_user_by_email(self): payload = deepcopy(self.payload) response = self.client.post('/api/v1/users', json=payload, content_type='application/json') self.assertEqual(200, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type_value(self, data, 'success', bool, True) assert_payload_field_type(self, data, 'data', dict) user_data = data['data'] assert_payload_field_type_value(self, user_data, 'id', int, self.user_1.id) assert_payload_field_type_value(self, user_data, 'type', str, 'users') attributes = user_data['attributes'] assert_payload_field_type_value(self, attributes, 'username', str, self.user_1.username) assert_payload_field_type_value(self, attributes, 'email', str, self.user_1.email) assert_payload_field_type_value(self, attributes, 'xp', int, self.user_1.xp) def test_endpoint_will_return_a(self): payload = {'email': '*****@*****.**'} response = self.client.post('/api/v1/users', json=payload, content_type='application/json') self.assertEqual(201, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type_value(self, data, 'user_action', str, 'created')
def test_user_model(self): user = User(username='******', email='*****@*****.**', xp=0) user.insert() self.assertIsInstance(user, User) self.assertIsNotNone(user.id) self.assertEqual('ian', user.username) self.assertEqual('*****@*****.**', user.email)
def test_user_model_missing_username(self): try: user = User(username=None, email='*****@*****.**', xp=0) user.insert() except IntegrityError: self.assertTrue(True) else: self.assertTrue(False)
def test_user_model_blank_email(self): try: user = User(username='******', email='', xp=0) user.insert() except IntegrityError: self.assertTrue(True) else: self.assertTrue(False) # pragma: no cover
def test_user_model_missing_email(self): try: user = User(username='******', email=None, xp=0) user.insert() except IntegrityError: self.assertTrue(True) else: # we should not end up in here self.assertTrue(False) # pragma: no cover
def test_user_can_be_deleted(self): user = User(username='******', email='*****@*****.**', xp=0) user.insert() user_2 = User(username='******', email='*****@*****.**', xp=0) user_2.insert() self.assertEqual(User.query.count(), 2) user.delete() self.assertEqual(User.query.count(), 1)
def test_user_model_unique_email(self): user = User(username='******', email='*****@*****.**', xp=0) user.insert() try: user = User(username='******', email='*****@*****.**', xp=0) user.insert() except IntegrityError: self.assertTrue(True) else: self.assertTrue(False) # pragma: no cover
def test_user_model_username_does_not_have_to_be_unique(self): user = User(username='******', email='*****@*****.**', xp=0) user.insert() try: user = User(username='******', email='*****@*****.**', xp=0) user.insert() except IntegrityError: self.assertTrue(False) else: self.assertTrue(True)
def test_for_user_without_user_quests(self): new_user = User(username='******', email="*****@*****.**", xp=0) new_user.insert() user_quests = new_user.user_quests.all().__len__() self.assertEqual(0, user_quests) response = self.client.get(f'/api/v1/users/{new_user.id}/quests?completion_status=false', content_type='application/json') user_quests = new_user.user_quests.all().__len__() self.assertEqual(3, user_quests)
def test_endpoint_sadpath_username_is_same_unique_email(self): user_1 = User(username='******', email='*****@*****.**', xp=0) user_1.insert() payload = {'username': '******', 'email': '*****@*****.**'} response = self.client.post('/api/v1/users', json=payload, content_type='application/json') data = json.loads(response.data.decode('utf-8')) self.assertEqual(201, response.status_code) assert_payload_field_type_value(self, data, 'user_action', str, 'created')
def test_user_can_update_attributes(self): user = User(username='******', email='*****@*****.**', xp=0) user.insert() self.assertEqual('ian', user.username) self.assertEqual('*****@*****.**', user.email) self.assertEqual(0, user.xp) user = User(username='******', email='*****@*****.**', xp=100) user.update() self.assertEqual('ian2', user.username) self.assertEqual('*****@*****.**', user.email) self.assertEqual(100, user.xp)
def test_user_quest_model(self): user = User(username='******', email='*****@*****.**', xp=0) user.insert() quest_1 = Quest(name="Make'a da pancake!", xp=5, level=1, encounter_req=3, type='active') quest_2 = Quest(name="Make'a da biscuit!", xp=10, level=2, encounter_req=1, type='active') db.session.add(quest_1) db.session.add(quest_2) db.session.commit() user_quest_1 = UserQuest(quest_id=quest_1.id, user_id=user.id, progress=1, completion_status=False) user_quest_2 = UserQuest(quest_id=quest_2.id, user_id=user.id, progress=1, completion_status=False) user_quest_1.insert() user_quest_2.insert() self.assertIsInstance(user_quest_1, UserQuest) self.assertEqual(UserQuest.query.count(), 2) self.assertIsNotNone(user_quest_1.id) self.assertEqual(1, user.id) self.assertEqual(user.id, user_quest_1.user_id) self.assertEqual(user.id, user_quest_2.user_id) self.assertEqual(1, user_quest_1.progress) self.assertEqual(False, user_quest_1.completion_status)
class PatchQuestsTest(unittest.TestCase): def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client() self.user_1 = User(username='******', email="*****@*****.**", xp=0) self.user_1.insert() self.user_2 = User(username='******', email="*****@*****.**", xp=0) self.user_2.insert() self.quest_1 = Quest(name="Make'a da pancake!", xp=5, level=1, encounter_req=3, type='active') self.quest_2 = Quest(name="Make'a da biscuit!", xp=10, level=2, encounter_req=1, type='active') self.quest_3 = Quest(name="Make'a da nuggets!", xp=15, level=3, encounter_req=5, type='active') self.quest_4 = Quest(name="Make'a da wings!", xp=15, level=3, encounter_req=5, type='passive') self.quest_1.insert() self.quest_2.insert() self.quest_3.insert() self.quest_4.insert() self.user_quest_1 = UserQuest(quest_id=self.quest_1.id, user_id=self.user_1.id, progress=1, completion_status=False) self.user_quest_2 = UserQuest(quest_id=self.quest_2.id, user_id=self.user_1.id, progress=1, completion_status=False) self.user_quest_3 = UserQuest(quest_id=self.quest_4.id, user_id=self.user_1.id, progress=5, completion_status=False) self.user_quest_4 = UserQuest(quest_id=self.quest_2.id, user_id=self.user_2.id, progress=1, completion_status=False) self.user_quest_1.insert() self.user_quest_2.insert() self.user_quest_3.insert() self.user_quest_4.insert() self.payload = {'quest_id': int(self.quest_1.id), 'progress': int(2)} self.payload2 = {'quest_id': int(self.quest_2.id), 'progress': int(2)} self.payload3 = {'quest_id': int(self.quest_4.id), 'progress': int(6)} self.payload4 = {'quest_id': int(self.quest_2.id), 'progress': int(1)} def tearDown(self): db.session.remove() db_drop_everything(db) self.app_context.pop() def test_happy_path_user_quest_progress_can_be_patched(self): payload = deepcopy(self.payload) response = self.client.patch(f'/api/v1/users/{self.user_1.id}/quests', json=payload, content_type='application/json') self.assertEqual(201, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type(self, data, 'data', dict) all_user_quest_data = data['data'] assert_payload_field_type_value(self, all_user_quest_data, 'id', int, self.user_quest_1.id) assert_payload_field_type_value(self, all_user_quest_data, 'type', str, 'user_quests') assert_payload_field_type(self, all_user_quest_data, 'attributes', dict) attributes = all_user_quest_data['attributes'] assert_payload_field_type_value(self, attributes, 'response', str, 'successful') assert_payload_field_type_value(self, attributes, 'progress', int, int(payload['progress'])) assert_payload_field_type_value(self, attributes, 'completion_status', bool, False) def test_happy_path_progress_change_can_update_completion_status(self): payload2 = deepcopy(self.payload2) response = self.client.patch(f'/api/v1/users/{self.user_1.id}/quests', json=payload2, content_type='application/json') self.assertEqual(201, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type(self, data, 'data', dict) all_user_quest_data = data['data'] assert_payload_field_type_value(self, all_user_quest_data, 'id', int, self.user_quest_2.id) assert_payload_field_type_value(self, all_user_quest_data, 'type', str, 'user_quests') assert_payload_field_type(self, all_user_quest_data, 'attributes', dict) attributes = all_user_quest_data['attributes'] assert_payload_field_type_value(self, attributes, 'response', str, 'successful') assert_payload_field_type_value(self, attributes, 'progress', int, int(payload2['progress'])) assert_payload_field_type_value(self, attributes, 'completion_status', bool, True) def test_sad_path_get_quests_for_user_no_params(self): response = self.client.patch(f"/api/v1/users/{self.user_1.id}/quests", content_type='application/json') self.assertEqual(400, response.status_code) # Come back and add in error messaging later def test_user_gains_xp_when_they_complete_quests(self): self.assertEqual(0, self.user_1.xp) payload2 = deepcopy(self.payload2) response = self.client.patch(f'/api/v1/users/{self.user_1.id}/quests', json=payload2, content_type='application/json') self.assertEqual(10, self.user_1.xp) def test_user_gets_new_quest_when_they_complete_quests(self): user_quests_total = self.user_1.user_quests.all().__len__() self.assertEqual(3, user_quests_total) payload2 = deepcopy(self.payload2) response = self.client.patch(f'/api/v1/users/{self.user_1.id}/quests', json=payload2, content_type='application/json') user_quests_total = self.user_1.user_quests.all().__len__() self.assertEqual(4, user_quests_total) new_user_quest = self.user_1.user_quests.all()[-1] self.assertEqual(3, new_user_quest.quest_id) quest = Quest.query.filter_by(id=new_user_quest.quest_id).one() self.assertEqual(self.quest_3.level, quest.level) self.assertEqual(self.quest_3.name, quest.name) self.assertEqual(self.quest_3.type, quest.type) def test_when_user_completes_all_levels_of_quest_path(self): payload3 = deepcopy(self.payload3) response = self.client.patch(f'/api/v1/users/{self.user_1.id}/quests', json=payload3, content_type='application/json') user_quests_total = self.user_1.user_quests.all().__len__() self.assertEqual(3, user_quests_total) user_xp = self.user_1.xp self.assertEqual(15, user_xp) user_quest_status = self.user_quest_3.completion_status self.assertEqual(True, user_quest_status) def test_sad_path_progress_doesnt_change(self): payload4 = deepcopy(self.payload4) response = self.client.patch(f'/api/v1/users/{self.user_2.id}/quests', json=payload4, content_type='application/json') self.assertEqual(404, response.status_code) def test_sad_path_user_does_not_exist(self): payload4 = deepcopy(self.payload4) response = self.client.patch(f'/api/v1/users/12/quests', json=payload4, content_type='application/json') self.assertEqual(404, response.status_code)
class GetQuestsTest(unittest.TestCase): def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client() self.user_1 = User(username='******', email="*****@*****.**", xp=1000000000) self.user_1.insert() self.quest_1 = Quest(name="Make'a da pancake!", xp=5, level=1, encounter_req=3, type='active') self.quest_2 = Quest(name="Make'a da biscuit!", xp=10, level=2, encounter_req=3, type='active') self.quest_3 = Quest(name="Make'a da nuggets!", xp=10, level=2, encounter_req=3, type='active') self.quest_4 = Quest(name="Make'a da cupcake!", xp=10, level=2, encounter_req=3, type='passive') self.quest_5 = Quest(name="Make'a da pizza!", xp=10, level=2, encounter_req=3, type='passive') self.quest_6 = Quest(name="Make'a da bacon!", xp=10, level=2, encounter_req=3, type='passive') self.quest_7 = Quest(name="Make'a da pie!", xp=10, level=2, encounter_req=3, type='supportive') self.quest_1.insert() self.quest_2.insert() self.quest_3.insert() self.quest_4.insert() self.quest_5.insert() self.quest_6.insert() self.quest_7.insert() self.user_quest_1 = UserQuest(quest_id=self.quest_1.id, user_id=self.user_1.id, progress=1, completion_status=False) self.user_quest_2 = UserQuest(quest_id=self.quest_2.id, user_id=self.user_1.id, progress=1, completion_status=True) self.user_quest_1.insert() self.user_quest_2.insert() def tearDown(self): db.session.remove() db_drop_everything(db) self.app_context.pop() def test_happy_path_get_quests_for_user_true(self): response = self.client.get(f'/api/v1/users/{self.user_1.id}/quests?completion_status=true', content_type='application/json') self.assertEqual(200, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type(self, data, 'data', dict) all_quest_data = data['data'] assert_payload_field_type_value(self, all_quest_data, 'id', str, 'Null') assert_payload_field_type_value(self, all_quest_data, 'type', str, 'quests') attributes = all_quest_data['attributes'] assert_payload_field_type(self, attributes, 'quests', list) quest = attributes['quests'][0] assert_payload_field_type(self, quest, 'quest_id_2', dict) quest_data = quest['quest_id_2'] assert_payload_field_type_value(self, quest_data, 'encounter_req', int, self.quest_2.encounter_req) assert_payload_field_type_value(self, quest_data, 'id', int, self.quest_2.id) assert_payload_field_type_value(self, quest_data, 'name', str, self.quest_2.name) assert_payload_field_type_value(self, quest_data, 'progress', int, self.user_quest_2.progress) assert_payload_field_type_value(self, quest_data, 'type', str, self.quest_2.type) assert_payload_field_type_value(self, quest_data, 'xp', int, self.quest_2.xp) assert_payload_field_type_value(self, quest_data, 'level', int, self.quest_2.level) def test_happy_path_get_quests_for_user_false(self): response = self.client.get(f'/api/v1/users/{self.user_1.id}/quests?completion_status=false', content_type='application/json') self.assertEqual(200, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type(self, data, 'data', dict) all_quest_data = data['data'] assert_payload_field_type_value(self, all_quest_data, 'id', str, 'Null') assert_payload_field_type_value(self, all_quest_data, 'type', str, 'quests') attributes = all_quest_data['attributes'] assert_payload_field_type(self, attributes, 'quests', list) quest = attributes['quests'][0] assert_payload_field_type(self, quest, 'quest_id_1', dict) quest_data = quest['quest_id_1'] assert_payload_field_type_value(self, quest_data, 'encounter_req', int, self.quest_1.encounter_req) assert_payload_field_type_value(self, quest_data, 'id', int, self.quest_1.id) assert_payload_field_type_value(self, quest_data, 'name', str, self.quest_1.name) assert_payload_field_type_value(self, quest_data, 'progress', int, self.user_quest_1.progress) assert_payload_field_type_value(self, quest_data, 'type', str, self.quest_1.type) assert_payload_field_type_value(self, quest_data, 'xp', int, self.quest_1.xp) assert_payload_field_type_value(self, quest_data, 'level', int, self.quest_1.level) def test_sad_path_get_quests_for_user_no_params(self): response = self.client.get(f"/api/v1/users/10/quests?completion_status=false", content_type='application/json') self.assertEqual(404, response.status_code) # TODO: BadRequestKeyError WHYY # response = self.client.get(f"/api/v1/users/{self.user_1.id}/quests?completion_status=2}") # breakpoint() # self.assertEqual(400, response.status_code) def test_for_user_without_user_quests(self): new_user = User(username='******', email="*****@*****.**", xp=0) new_user.insert() user_quests = new_user.user_quests.all().__len__() self.assertEqual(0, user_quests) response = self.client.get(f'/api/v1/users/{new_user.id}/quests?completion_status=false', content_type='application/json') user_quests = new_user.user_quests.all().__len__() self.assertEqual(3, user_quests)
def test_user_model_trimmed_email(self): user = User(username='******', email=' [email protected] ', xp=0) user.insert() self.assertEqual('*****@*****.**', user.email)
class GetAllUsersTest(unittest.TestCase): def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client() self.user_1 = User(username='******', email='e1', xp=0) self.user_1.insert() self.user_2 = User(username='******', email='e2', xp=0) self.user_2.insert() def tearDown(self): db.session.remove() db_drop_everything(db) self.app_context.pop() def test_happypath_get_all_users(self): response = self.client.get( '/api/v1/users', content_type='application/json' ) self.assertEqual(200, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type_value(self, data, 'success', bool, True) assert_payload_field_type(self, data, 'data', dict) users_data = data['data'] assert_payload_field_type_value( self, users_data, 'type', str, 'users' ) attributes = users_data['attributes'] assert_payload_field_type_value( self, attributes[0], 'id', int, self.user_1.id ) assert_payload_field_type_value( self, attributes[1], 'id', int, self.user_2.id ) assert_payload_field_type_value( self, attributes[0], 'username', str, self.user_1.username ) assert_payload_field_type_value( self, attributes[1], 'username', str, self.user_2.username ) assert_payload_field_type_value( self, attributes[0], 'email', str, self.user_1.email ) assert_payload_field_type_value( self, attributes[1], 'email', str, self.user_2.email ) assert_payload_field_type_value( self, attributes[0], 'xp', int, self.user_1.xp ) assert_payload_field_type_value( self, attributes[1], 'xp', int, self.user_2.xp ) def test_endpoint_sadpath_bad_email_user(self): response = self.client.get( '/api/v1', content_type='application/json' ) self.assertEqual(404, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type_value(self, data, 'error', int, 404) assert_payload_field_type_value(self, data, 'success', bool, False) assert_payload_field_type_value( self, data, 'message', str, 'resource not found' )
class GetEncountersByQuest(unittest.TestCase): def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.client = self.app.test_client() self.user_1 = User(username='******', email="*****@*****.**", xp=1000000000) self.user_1.insert() self.quest_1 = Quest(name="Make'a da pancake!", xp=5, level=1, encounter_req=3, type='active') self.quest_2 = Quest(name="Make'a da biscuit!", xp=10, level=2, encounter_req=3, type='active') self.quest_1.insert() self.quest_2.insert() self.user_quest_1 = UserQuest(quest_id=self.quest_1.id, user_id=self.user_1.id, progress=1, completion_status=False) self.user_quest_2 = UserQuest(quest_id=self.quest_2.id, user_id=self.user_1.id, progress=1, completion_status=True) self.user_quest_1.insert() self.user_quest_2.insert() self.encounter_1 = Encounter( monster_image= 'https://images.huffingtonpost.com/2015-02-05-trollersTroll-thumb.jpg', quest_id=self.quest_1.id, progress=1) self.encounter_1.insert() self.encounter_2 = Encounter( monster_image= 'https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSdIfSV09BWeNuPejZM4txwTFJJKikYV_WMLg&usqp=CAU', quest_id=self.quest_1.id, progress=2) self.encounter_2.insert() self.action_1 = Action(encounter_id=self.encounter_1.id, description='Send a message to a recruiter') self.action_2 = Action(encounter_id=self.encounter_1.id, description='Apply to a Job') self.action_3 = Action( encounter_id=self.encounter_2.id, description='Schedule a coffee meetup with a target Company') self.action_1.insert() self.action_2.insert() self.action_3.insert() def tearDown(self): db.session.remove() db_drop_everything(db) self.app_context.pop() def test_happy_path_get_encounters_for_quest_by_progress(self): response = self.client.get( f'/api/v1/quests/{self.quest_1.id}/encounters?progress=2', content_type='application/json') self.assertEqual(200, response.status_code) data = json.loads(response.data.decode('utf-8')) assert_payload_field_type(self, data, 'data', dict) encounter_data = data['data'] assert_payload_field_type_value(self, encounter_data, 'id', int, self.encounter_2.id) assert_payload_field_type_value(self, encounter_data, 'type', str, 'encounters') attributes = encounter_data['attributes'] assert_payload_field_type(self, encounter_data, 'attributes', dict) assert_payload_field_type_value(self, attributes, 'progress', int, self.encounter_2.progress) assert_payload_field_type_value(self, attributes, 'monster_image', str, self.encounter_2.monster_image) assert_payload_field_type(self, attributes, 'actions', list) actions = attributes['actions'] assert_payload_field_type_value(self, actions[0], 'id', int, self.action_3.id) assert_payload_field_type_value(self, actions[0], 'description', str, self.action_3.description) def test_sad_path_get_encounters_for_quest_when_no_progress_given(self): response = self.client.get( f'/api/v1/quests/{self.quest_1.id}/encounters?progress=', content_type='application/json') self.assertEqual(500, response.status_code) def test_sad_path_get_encounters_for_quest_when_wrong_progress_given(self): response = self.client.get( f'/api/v1/quests/{self.quest_1.id}/encounters?progress=896', content_type='application/json') self.assertEqual(404, response.status_code)