def test_study_group_list(self): user = User(username='******', email='*****@*****.**', password='******', first_name="Unit", last_name="Test") user.save() profile = Profile(user=user, reports=0, user_type=0) profile.save() c = Client() course = Courses(course_number=309, course_department="COM S", course_name="Software Project") course.save() response = c.post('/api/studygroup/create/', json.dumps({"course_id": 1}), content_type="application/json") self.assertEqual(response.status_code, 200) self.assertEqual( StudyGroup.objects.get(course=1).course.course_name, 'Software Project') self.assertEqual(response.content, b'Software Project') response = c.post('/api/studygroup/join/', json.dumps({ "id": 1, "studygroup_id": 1 }), content_type="application/json") studentgroup = StudentsStudyGroups.objects.get( user=user, group=StudyGroup.objects.get(id=1)) self.assertEqual(1, studentgroup.user.id) self.assertEqual(1, studentgroup.group.id) response = c.get('/api/studygroup/list/') self.assertJSONEqual(response.content, [{ "id": 1, "course_department": "COM S", "course_number": 309, "course_name": "Software Project", "members": [{ "id": 1, "username": "******", "email": "*****@*****.**", "first_name": "Unit", "last_name": "Test" }] }])
def test_join_study_group(self): user = User(username='******', email='*****@*****.**', password='******', first_name="Unit", last_name="Test") user.save() profile = Profile(user=user, reports=0, user_type=0) profile.save() c = Client() course = Courses(course_number=309, course_department="COM S", course_name="Software Project") course.save() response = c.post('/api/studygroup/create/', json.dumps({"course_id": 1}), content_type="application/json") self.assertEqual(response.status_code, 200) self.assertEqual( StudyGroup.objects.get(course=1).course.course_name, 'Software Project') self.assertEqual(response.content, b'Software Project') response = c.post('/api/studygroup/join/', json.dumps({ "id": 1, "studygroup_id": 1 }), content_type="application/json") studentgroup = StudentsStudyGroups.objects.get( user=user, group=StudyGroup.objects.get(id=1)) self.assertEqual(1, studentgroup.user.id) self.assertEqual(1, studentgroup.group.id) response = c.post('/api/studygroup/join/', json.dumps({ "id": 1, "studygroup_id": 1 }), content_type="application/json") self.assertEqual(response.content, b'You are already a member of this group.')
def create_user(request): """ Creates a new user. The username, first_name, last_name, email, password, and user_type of the user are passed in from the client as json. :param request: The request made from the client. :return: An HttpResponse with the username of the user created (used for testing). """ create_username = json.loads(request.body.decode("utf-8"))['username'] create_first_name = json.loads(request.body.decode("utf-8"))['first_name'] create_last_name = json.loads(request.body.decode("utf-8"))['last_name'] create_email = json.loads(request.body.decode("utf-8"))['email'] create_password = json.loads(request.body.decode("utf-8"))['password'] new_user = User(username=create_username, first_name=create_first_name, last_name=create_last_name, email=create_email, password=create_password) new_user.set_password(new_user.password) # user_type = json.loads(request.body.decode("utf-8"))['user_type'] new_user.save() new_profile = Profile(user=new_user, reports=0, user_type=0) new_profile.save() return HttpResponse(new_user.username)