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_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_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 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_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_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_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_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_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_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 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_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)
def test_unjoin_group(self): header = self.headers["test1"] profile_id_search = self.get_profile_ids(header) selected_profile_id = random.choice(profile_id_search[0]) selected_non_profile_id = random.choice(profile_id_search[1]) group_endpoint = f"/groups/?profile_id={selected_profile_id}" groups = [] group_search = Helpers.get_request(group_endpoint, header=header) for group in group_search[1]: groups.append(group["groups"]["group_id"]) non_groups = [i for i in range(1, 11) if i not in groups] body1 = {"profile_id": f"{selected_profile_id}"} body2 = {"profile_id": f"{selected_non_profile_id}"} endpoint1 = f"/groups/{groups[0]}/unjoin" endpoint2 = f"/groups/{non_groups[0]}/unjoin" 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(data["profile_id"], selected_profile_id) self.assertEqual(data["group_id"], groups[0]) self.assertEqual(response2.status_code, 401) self.assertIsNone(data2) self.assertEqual(response3.status_code, 404) self.assertIsNone(data3)
def test_group_update(self): header = self.headers["test3"] profile_ids = Helpers.get_profile_id(header) while len(profile_ids) == 0: self.tearDown() self.setUp() profile_ids = Helpers.get_profile_id(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]}" body1 = { "name": "New group name", "description": "New group description" } body2 = {"name": "", "description": ""} response, data = Helpers.patch_request(endpoint1, header, body1) response2, data2 = Helpers.patch_request(endpoint2, header, body1) response3, data3 = Helpers.patch_request(endpoint1, header, body2) self.assertEqual(response.status_code, 200) self.assertTrue(response2.status_code in [401, 404]) self.assertEqual(response3.status_code, 400) self.assertEqual(data["name"], "New group name") self.assertEqual(data["description"], "New group description") self.assertIsNone(data2) self.assertIsInstance(data3, dict)