def test_remove_content(self): header = self.headers["test5"] endpoint1, endpoint2 = self.generate_profile_endpoints(header) endpoint1 = endpoint1 + "/unrecommend" endpoint2 = endpoint2 + "/unrecommend" unrecommended, available_content = self.get_unrecommended_content( endpoint1, header) body1 = {"content_id": random.choice(unrecommended)} body2 = {"content_id": random.choice(available_content)} response, data = Helpers.delete_request(endpoint1, header=header, body=body1) response2, data2 = Helpers.delete_request(endpoint2, header=header, body=body1) response3, data3 = Helpers.delete_request(endpoint1, header=header, body=body2) self.assertEqual(response.status_code, 200) self.assertEqual(len(data), 1) self.assertEqual(response2.status_code, 404) self.assertIsNone(data2) self.assertEqual(response3.status_code, 404) self.assertIsNone(data3)
def test_create_group(self): header = self.headers["test1"] endpoint1, endpoint2 = self.generate_group_endpoints(header) body1 = { "name": "New group", "description": "New netflix group with friends" } body2 = {"name": "", "description": ""} response, data = Helpers.post_request(endpoint1, header=header, body=body1) response2, data2 = Helpers.post_request(endpoint2, header=header, body=body1) response3, data3 = Helpers.post_request(endpoint1, header=header, body=body2) self.assertEqual(response.status_code, 200) self.assertIsInstance(data, dict) self.assertEqual(response2.status_code, 404) self.assertIsNone(data2) self.assertEqual(response3.status_code, 400) self.assertEqual(data3["name"], ['Shorter than minimum length 1.'])
def test_group_delete(self): header = self.headers["test4"] profile_ids, non_profile_ids = self.get_profile_ids(header) groups = Helpers.get_request(f"/groups/?profile_id={profile_ids[0]}", header=header) group_ids = [] for group in groups[1]: if group["admin"]: group_ids.append(group["groups"]["group_id"]) non_group_ids = [i for i in range(1, 11) if i not in group_ids] endpoint1 = f"/groups/{group_ids[0]}?profile_id={profile_ids[0]}" endpoint2 = f"/groups/{non_group_ids[0]}?profile_id={profile_ids[0]}" endpoint3 = f"/groups/{group_ids[0]}?profile_id={non_profile_ids[0]}" response, data = Helpers.delete_request(endpoint1, header=header) response2, data2 = Helpers.delete_request(endpoint2, header=header) response3, data3 = Helpers.delete_request(endpoint3, header=header) self.assertEqual(response.status_code, 200) self.assertIsInstance(data, dict) self.assertEqual(response2.status_code, 401) self.assertIsNone(data2) self.assertEqual(response3.status_code, 404) self.assertIsNone(data3)
def setUp(self): response = RequestHandler.get_request(self, endpoint1) user_id = Helpers.json_parser(self, response.text, 'id') post_response = RequestHandler.get_request(self, endpoint3 + str(user_id[0])) global post_id post_id = Helpers.json_parser(self, post_response.text, 'id')
def test_validate_comment_emails(self): for comment_id in post_id: comment_response = RequestHandler.get_request( self, endpoint2 + str(comment_id)) comment_emails = Helpers.json_parser(self, comment_response.text, 'email') for comment_email in comment_emails: self.assertTrue(Helpers.is_email_valid(self, comment_email))
def test_remove_member(self): header = self.headers["test2"] profile_id_search = self.get_profile_ids(header) selected_profile_id = random.choice(profile_id_search[0]) group = GroupMembers.query.filter_by(profile_id=selected_profile_id, admin=True).first() group_id = group.group_id non_group_ids = [i for i in range(1, 11) if i != group_id] group_members_search = GroupMembers.query.filter_by(group_id=group_id, admin=False) member_ids = [] for group in group_members_search: member_ids.append(group.profile_id) non_member_ids = [i for i in range(1, 11) if i not in member_ids] endpoint1 = f"/groups/{random.choice(non_group_ids)}/remove_member" endpoint2 = f"/groups/{group_id}/remove_member" body1 = { "admin_id": f"{selected_profile_id}", "member_id": f"{random.choice(member_ids)}" } body2 = { "admin_id": f"{random.choice(member_ids)}", "member_id": f"{selected_profile_id}" } body3 = { "admin_id": f"{selected_profile_id}", "member_id": f"{random.choice(non_member_ids)}" } response, data = Helpers.delete_request(endpoint1, header=header, body=body1) response2, data2 = Helpers.delete_request(endpoint2, header=header, body=body2) response3, data3 = Helpers.delete_request(endpoint2, header=header, body=body3) response4, data4 = Helpers.delete_request(endpoint2, header=header, body=body1) self.assertTrue(response.status_code in [401, 404]) self.assertIsNone(data) self.assertTrue(response2.status_code in [401, 404]) self.assertIsNone(data2) self.assertEqual(response3.status_code, 404) self.assertIsNone(data3) self.assertEqual(response4.status_code, 200) self.assertEqual(data4["status"], "removed from group")
def get_profile_ids(self, header): profile_ids = Helpers.get_profile_id(header) while len(profile_ids) == 0: self.tearDown() self.setUp() profile_ids = Helpers.get_profile_id(header) non_profile_ids = [i for i in range(1, 11) if i not in profile_ids] return (profile_ids, non_profile_ids)
def test_show_profiles(self): endpoint = "/profiles/" response, data = Helpers.get_request(endpoint, header=self.headers["test1"]) response2, data2 = Helpers.get_request(endpoint, header=self.headers["fakeuser"]) self.assertEqual(response.status_code, 200) self.assertIsInstance(data, list) self.assertEqual(response2.status_code, 422)
def test_get_profile_by_id(self): header = self.headers["test2"] endpoint1, endpoint2 = self.generate_profile_endpoints(header) response, data = Helpers.get_request(endpoint1, header=header) response2, data2 = Helpers.get_request(endpoint2, header=header) self.assertEqual(response.status_code, 200) self.assertEqual(response2.status_code, 404) self.assertEqual(data["user"]["email"], "*****@*****.**") self.assertIsNone(data2)
def test_get_groups(self): header = self.headers["test2"] endpoint1, endpoint2 = self.generate_group_endpoints(header) response, data = Helpers.get_request(endpoint1, header=header) response2, data2 = Helpers.get_request(endpoint2, header=header) self.assertEqual(response.status_code, 200) self.assertEqual(response2.status_code, 404) self.assertIsInstance(data, list) self.assertIsNone(data2)
def test_delete_profile(self): header = self.headers["test3"] endpoint1, endpoint2 = self.generate_profile_endpoints(header) response, data = Helpers.delete_request(endpoint1, header=header) response2, data2 = Helpers.delete_request(endpoint2, header=header) self.assertEqual(response.status_code, 200) self.assertEqual(response2.status_code, 404) self.assertEqual(data["user"]["user_id"], 3) self.assertIsNone(data2)
def test_get_group_content(self): endpoint = "/admin/groupcontent" header = self.headers["Admin2"] header2 = self.headers["fakeadmin"] response, data = Helpers.get_request(endpoint, header) response2, data2 = Helpers.get_request(endpoint, header2) self.assertEqual(response.status_code, 200) self.assertEqual(len(data), 30) self.assertEqual(response2.status_code, 422)
def generate_profile_endpoints(self, header): profile_ids = Helpers.get_profile_id(header) while len(profile_ids) == 0: self.tearDown() self.setUp() profile_ids = Helpers.get_profile_id(header) non_profile_ids = [i for i in range(1, 11) if i not in profile_ids] endpoint1 = f"/profiles/{random.choice(profile_ids)}" endpoint2 = f"/profiles/{random.choice(non_profile_ids)}" return (endpoint1, endpoint2)
def test_get_content(self): header = self.headers["test1"] header2 = self.headers["fakeuser"] endpoint = "/content/" response, data = Helpers.get_request(endpoint, header=header) response2, data2 = Helpers.get_request(endpoint, header=header2) self.assertEqual(response.status_code, 200) self.assertIsInstance(data, list) self.assertEqual(len(data), 30) self.assertEqual(response2.status_code, 422)
def test_get_user(self): endpoint = "/users/" response, data = Helpers.get_request(endpoint, self.headers["test1"]) response2, data2 = Helpers.get_request(endpoint) response3, data3 = Helpers.get_request( endpoint, self.headers["fakeuser"]) self.assertEqual(response.status_code, 200) self.assertEqual(data["user_id"], 1) self.assertEqual(len(data), 3) self.assertEqual(response2.status_code, 401) self.assertEqual(response3.status_code, 422)
def test_get_group_by_id(self): header = self.headers["test3"] endpoint1 = f"/groups/{random.randrange(1, 11)}" endpoint2 = "/groups/31" response, data = Helpers.get_request(endpoint1, header=header) response2, data2 = Helpers.get_request(endpoint2, header=header) self.assertEqual(response.status_code, 200) self.assertEqual(response2.status_code, 404) self.assertEqual(len(data), 4) self.assertIsNone(data2)
def test_remove_content(self): header = self.headers["test4"] profile_ids, non_profile_ids = self.get_profile_ids(header) group_search = GroupMembers.query.filter_by(profile_id=profile_ids[0]) group_ids = [] for group in group_search: group_ids.append(group.groups.group_id) non_group_ids = [i for i in range(1, 11) if i not in group_ids] group_content_ids = [] group_content_search = Group.query.get(group_ids[0]) for content in group_content_search.content: group_content_ids.append(content.content_id) non_group_content_ids = [ i for i in range(1, 31) if i not in group_content_ids ] endpoint1 = f"/groups/{group_ids[0]}/content?profile_id={profile_ids[0]}" # noqa: E501 endpoint2 = f"/groups/{non_group_ids[0]}/content?profile_id={profile_ids[0]}" # noqa: E501 body1 = {"content_id": f"{non_group_content_ids[0]}"} body2 = {"content_id": f"{group_content_ids[0]}"} body3 = {"content_id": "31"} response, data = Helpers.delete_request(endpoint1, header=header, body=body1) response2, data2 = Helpers.delete_request(endpoint2, header=header, body=body2) response3, data3 = Helpers.delete_request(endpoint1, header=header, body=body3) response4, data4 = Helpers.delete_request(endpoint1, header=header, body=body2) data4_content = [] for content in data4["content"]: data4_content.append(content["content_id"]) self.assertEqual(response.status_code, 404) self.assertIsNone(data) self.assertEqual(response2.status_code, 401) self.assertIsNone(data2) self.assertEqual(response3.status_code, 404) self.assertIsNone(data3) self.assertEqual(response4.status_code, 200) self.assertTrue(group_content_ids[0] not in data4_content)
def test_unrecommended_content(self): header = self.headers["test4"] endpoint1, endpoint2 = self.generate_profile_endpoints(header) endpoint1 = endpoint1 + "/unrecommend" endpoint2 = endpoint2 + "/unrecommend" response, data = Helpers.get_request(endpoint1, header=header) response2, data2 = Helpers.get_request(endpoint2, header=header) self.assertEqual(response.status_code, 200) self.assertEqual(response2.status_code, 404) self.assertEqual(len(data), 2) self.assertIsNone(data2)
def test_delete_content(self): selected_content = random.randrange(1, 31) endpoint1 = f"/admin/content/{selected_content}" endpoint2 = "/admin/content/31" header = self.headers["Admin2"] response, data = Helpers.delete_request(endpoint1, header=header) response2, data2 = Helpers.delete_request(endpoint2, header=header) self.assertEqual(response.status_code, 200) self.assertEqual(data["content_id"], selected_content) self.assertEqual(response2.status_code, 404) self.assertIsNone(data2)
def test_admin_login(self): endpoint = "admin/login" body1 = {"username": "******", "password": "******"} body2 = {"username": "******", "password": "******"} response, data = Helpers.post_request(endpoint, body=body1) response2, data2 = Helpers.post_request(endpoint, body=body2) self.assertEqual(response.status_code, 200) self.assertIn("token", data) self.assertIsInstance(data["token"], str) self.assertEqual(response2.status_code, 401) self.assertIsNone(data2)
def test_get_users(self): endpoint = "/admin/users" header = self.headers["Admin1"] header2 = self.headers["fakeadmin"] response, data = Helpers.get_request(endpoint, header) response2, data2 = Helpers.get_request(endpoint) response3, data3 = Helpers.get_request(endpoint, header2) self.assertEqual(response.status_code, 200) self.assertEqual(len(data), 5) self.assertEqual(response2.status_code, 401) self.assertEqual(response3.status_code, 422)
def test_users_register(self): endpoint = "/users/register" body = { "email": "*****@*****.**", "password": "******", "subscription_status": "1"} response, data = Helpers.post_request(endpoint, body=body) response2, data2 = Helpers.post_request(endpoint, body=body) self.assertEqual(response.status_code, 200) self.assertEqual(data["user_id"], 6) self.assertEqual(data["email"], "*****@*****.**") self.assertEqual(data["subscription_status"], True) self.assertEqual(response2.status_code, 401) self.assertIsNone(data2)
def test_delete_user(self): endpoint = "/users/" header = self.headers["test5"] before_delete = Helpers.get_request(endpoint, header=header) delete = Helpers.delete_request(endpoint, header=header) after_delete = Helpers.get_request(endpoint, header=header) delete2 = Helpers.delete_request(endpoint, header=header) self.assertEqual(before_delete[0].status_code, 200) self.assertEqual(before_delete[1]["user_id"], 5) self.assertEqual(delete[0].status_code, 200) self.assertEqual(delete[1]["user_id"], 5) self.assertEqual(after_delete[0].status_code, 404) self.assertIsNone(after_delete[1]) self.assertEqual(delete2[0].status_code, 404)
def test_create_content(self): endpoint = "/admin/content" header = self.headers["Admin1"] body1 = {"title": "Movie 1", "genre": "Action", "year": "2019"} body2 = {"title": "", "genre": "", "year": ""} response, data = Helpers.post_request(endpoint, header=header, body=body1) response2, data2 = Helpers.post_request(endpoint, header=header, body=body2) self.assertEqual(response.status_code, 200) self.assertEqual(data["content_id"], 31) self.assertEqual(response2.status_code, 400)
def test_validate_empty_name(self): for comment_id in post_id: comment_response = RequestHandler.get_request( self, endpoint2 + str(comment_id)) comment_names = Helpers.json_parser(self, comment_response.text, 'name') for comment_name in comment_names: self.assertTrue(not str(comment_name).__eq__(''))
def test_validate_empty_body(self): for comment_id in post_id: comment_response = RequestHandler.get_request( self, endpoint2 + str(comment_id)) comment_body = Helpers.json_parser(self, comment_response.text, 'body') for body in comment_body: self.assertTrue(not str(body).__eq__(''))
def get_unrecommended_content(self, endpoint, header): get_content = Helpers.get_request(endpoint, header=header) unrecommended = [] for content in get_content[1]: unrecommended.append(content["content_id"]) available_content = [i for i in range(1, 31) if i not in unrecommended] return (unrecommended, available_content)
def test_create_profile(self): endpoint = "/profiles/create" body = {"name": "New profile", "restrictions": "M"} body2 = {"name": "New profile", "restrictions": "A"} response, data = Helpers.post_request(endpoint, body=body, header=self.headers["test3"]) response2, data2 = Helpers.post_request(endpoint, body=body2, header=self.headers["test4"]) self.assertEqual(response.status_code, 200) self.assertEqual(response2.status_code, 400) self.assertIsInstance(data, dict) self.assertEqual(data["profile_id"], 11) self.assertEqual(data["name"], "New profile") self.assertEqual(data["restrictions"], "M")
def test_users_login(self): endpoint = "/users/login" body = { "email": "*****@*****.**", "password": "******" } body2 = { "email": "*****@*****.**", "password": "******" } response, data = Helpers.post_request(endpoint, body=body) response2, data2 = Helpers.post_request(endpoint, body=body2) self.assertEqual(response.status_code, 200) self.assertIn("token", data) self.assertIsInstance(data["token"], str) self.assertEqual(response2.status_code, 401) self.assertIsNone(data2)
def test_update_user(self): endpoint = "/users/" body = { "email": "*****@*****.**", "subscription_status": "false" } body2 = { "email": "*****@*****.**", "subscription_status": "true" } response, data = Helpers.patch_request( endpoint, self.headers["test1"], body) response2, data2 = Helpers.patch_request( endpoint, self.headers["test2"], body2) user1 = Helpers.get_request(endpoint, self.headers["test1"]) user2 = Helpers.get_request(endpoint, self.headers["test2"]) self.assertEqual(response.status_code, 200) self.assertEqual(response2.status_code, 200) self.assertEqual(len(data), 3) self.assertEqual(user1[1]["email"], "*****@*****.**") self.assertEqual(user2[1]["subscription_status"], True)