def test_group_members_delete(self): group = Group(name="test_group", manager=self.manager) group.save() user = User.objects.create_user( "test-receiver", "1234", email="", salt="", role=User.Role.Coder) coder = Coder(user=user, group=group) coder.save() response = self.client.delete( f"/api/group/{group.id}/members/{user.id}/") self.assertEqual(response.status_code, 204) coder = Coder.objects.get(user=user) self.assertEqual(coder.group, None) response = self.client.delete(f"/api/group/{group.id}/members/100/") self.assertEqual(response.status_code, 400) response = self.client.post( f"/api/group/{group.id}/members/{user.id}/", {}) self.assertEqual(response.status_code, 405) group = Group(name="test_group2", manager=self.manager2) group.save() response = self.client.delete( f"/api/group/{group.id}/members/{user.id}/") self.assertEqual(response.status_code, 400)
def test_group_members_get(self): group = Group(name="test_group", manager=self.manager) group.save() user = User.objects.create_user( "test-receiver", "1234", email="", salt="", role=User.Role.Coder) coder = Coder(user=user, group=group) coder.save() response = self.client.get(f"/api/group/{group.pk}/members/") self.assertEqual(response.status_code, 200) response = self.client.post(f"/api/group/{group.pk}/members/", {}) self.assertEqual(response.status_code, 405) group = Group(name="test2_group", manager=self.manager2) group.save() response = self.client.get(f"/api/group/{group.pk}/members/") self.assertEqual(response.status_code, 400) self.client.logout() self.client.login(username="******", password="******") response = self.client.get(f"/api/group/{group.pk}/members/") self.assertEqual(response.status_code, 400)
def create_single_coder(num, style, problems): user = User( username=f"user{num}", password= "******", email=f"user{num}@test.com", salt="123", role=1) user.save() coding_style = CodingStyle(style=style, UM_value=0.5, TI_value=0.5, RT_value=0.5, JC_value=0.5) coding_style.save() coder = Coder(user=user, style=coding_style) coder.save() solutions = [] for problem in problems: # 여기서부터 변경했습니다. code = read_solution_example(randrange(0, 100), problem.pid) erase_cnt = randrange(200, 300) elapsed_time = randrange(13, 100) evalutaion = uniform(0, 1) solution = Solution(problem=problem, code=code, erase_cnt=erase_cnt, elapsed_time=elapsed_time, author_id=user.id, evalutaion=evalutaion) solution.save() SolutionReport(solution=solution, author=user, title=f"{problem.pid}_report", code=code).save() solutions.append(solution) user_report = UserReport(author=user, solution1=solutions[0].code, solution2=solutions[1].code, solution3=solutions[2].code, title=f"{user.username}'s report") user_report.to_dict() user_report.save()
def test_get_problem_by_objective(self): coder = Coder(user=self.user) coder.save() problem = Problem(desc="For test", input_desc="For test", output_desc="Fore test", pid="ITP1_6_B", objective=Problem.ProblemObjective.UM) problem.save() response = self.client.get("/api/problem/objective/") self.assertEqual(response.status_code, 200) response = self.client.post("/api/problem/objective/", {}) self.assertEqual(response.status_code, 405)
def signup(request): if request.method == 'POST': try: req_data = json.loads(request.body.decode()) username = req_data["username"] email = req_data["email"] password = req_data["password"] role = req_data["role"] except (KeyError, JSONDecodeError) as error: return HttpResponseBadRequest(error) try: same_name = User.objects.filter(username=username).count() same_email = User.objects.filter(email=email).count() if same_name > 0 or same_email > 0: raise IntegrityError() user = User.objects.create_user( username=username, email=email, salt="", role=role, password=password) if role == User.Role.Coder: Coder(user=user).save() elif role == User.Role.Manager: Manager(user=user).save() elif role == User.Role.Researcher: Researcher(user=user).save() except IntegrityError: return HttpResponse(status=409) return HttpResponse(status=201) return HttpResponseNotAllowed(['POST'])
def test_group_relations(self): group = Group(name="test_group", manager=self.manager) group.save() group2 = Group(name="test2_group", manager=self.manager2) group2.save() user = User.objects.create_user( "test-receiver", "1234", email="", salt="", role=User.Role.Coder) style = CodingStyle( style=CodingStyle.Style.MTRJ, UM_value=1.0, TI_value=1.0, RT_value=1.0, JC_value=1.0) style.save() coder = Coder(user=user, style=style, group=group) coder.save() self.client.logout() self.client.login(username="******", password="******") response = self.client.get(f"/api/group/{group.pk}/find_relation/") self.assertEqual(response.status_code, 400) self.client.logout() self.client.login(username="******", password="******") response = self.client.get(f"/api/group/{group.pk}/find_relation/") self.assertEqual(response.status_code, 200) response = self.client.get(f"/api/group/{group2.pk}/find_relation/") self.assertEqual(response.status_code, 400) Group.objects.get(name="test_group").delete() response = self.client.get(f"/api/group/{group.pk}/find_relation/") self.assertEqual(response.status_code, 400) response = self.client.delete(f"/api/group/{group.pk}/find_relation/") self.assertEqual(response.status_code, 405)
def test_group_invite(self): response = self.client.get("/api/group/invite/") self.assertEqual(response.status_code, 400) user = User.objects.create_user( "test-receiver", "1234", email="", salt="", role=User.Role.Coder) coder = Coder(user=user) coder.save() response = self.client.post( '/api/group/', json.dumps({"name": "test_group"}), content_type="application/json") group_id = json.loads(response.content)["id"] response = self.client.post( "/api/group/invite/", json.dumps({"receiver": user.pk, "group_id": group_id}), content_type="application/json") self.assertEqual(response.status_code, 201) invitation_id = json.loads(response.content)["id"] response = self.client.get( f"/api/group/invite/accept/{invitation_id}/") self.assertEqual(response.status_code, 400) self.client.logout() self.client.login(username="******", password="******") response = self.client.get( f"/api/group/invite/accept/{invitation_id}/") self.assertEqual(response.status_code, 204) response = self.client.post( f"/api/group/invite/") self.assertEqual(response.status_code, 400) response = self.client.delete( f"/api/group/invite/") self.assertEqual(response.status_code, 405) response = self.client.get("/api/group/invite/") self.assertEqual(response.status_code, 200) self.client.logout() self.client.login(username="******", password="******") response = self.client.delete( f"/api/group/invite/accept/{invitation_id}/") self.assertEqual(response.status_code, 400) self.client.logout() self.client.login(username="******", password="******") response = self.client.delete( f"/api/group/invite/accept/{invitation_id}/") self.assertEqual(response.status_code, 204) self.assertEqual(Invitation.objects.count(), 0) response = self.client.delete( f"/api/group/invite/accept/{invitation_id}/") self.assertEqual(response.status_code, 400) someone_else = User.objects.create_user( username="******", password="******", email="123@112", salt="", role=User.Role.Coder) self.client.logout() self.client.login(username="******", password="******") response = self.client.post( "/api/group/invite/", json.dumps({"receiver": user.pk, "group_id": group_id}), content_type="application/json") invitation_id = json.loads(response.content)["id"] Group.objects.get(name="test_group").delete() self.client.logout() self.client.login(username="******", password="******") response = self.client.get( f"/api/group/invite/accept/{invitation_id}/") self.assertEqual(response.status_code, 400) response = self.client.post( f"/api/group/invite/accept/{invitation_id}/", {}) self.assertEqual(response.status_code, 405)
def test_get_coders_by_style(self): user1 = User.objects.create_user(username="******", password="******", email="*****@*****.**", salt="123", role=1) user2 = User.objects.create_user(username="******", password="******", email="*****@*****.**", salt="123", role=1) user3 = User.objects.create_user(username="******", password="******", email="*****@*****.**", salt="123", role=1) coding_style1 = CodingStyle(style=CodingStyle.Style.UITC, UM_value=0.0, TI_value=0.0, RT_value=0.0, JC_value=0.0) coding_style1.save() coding_style2 = CodingStyle(style=CodingStyle.Style.UITC, UM_value=0.0, TI_value=0.0, RT_value=0.0, JC_value=0.0) coding_style2.save() coding_style3 = CodingStyle(style=CodingStyle.Style.UITC, UM_value=0.0, TI_value=0.0, RT_value=0.0, JC_value=0.0) coding_style3.save() coder1 = Coder(user=user1, style=coding_style1) coder1.save() coder2 = Coder(user=user2, style=coding_style2) coder2.save() coder3 = Coder(user=user3, style=coding_style3) coder3.save() expected_response = [{ "user_id": user1.pk, "username": "******", "style": to_dict(coding_style1), "group": None }, { "user_id": user2.pk, "username": "******", "style": to_dict(coding_style2), "group": None }, { "user_id": user3.pk, "username": "******", "style": to_dict(coding_style3), "group": None }] client = Client() res = client.get("/api/analysis/style/8/") self.assertEqual(res.content.decode(), json.dumps(expected_response))