Пример #1
0
 def test_get_school(self):
     """
     Test getting a user's school. Just testing the database object, not code.
     """
     sample.sample_data_one_user()
     me = User.objects.get(email="*****@*****.**")
     self.assertEqual("University of Illinois Urbana-Champaign", me.school.name, "Failed getting school name.")
Пример #2
0
 def test_create_new_user(self):
     """
     Try cretaing a new user, then try loggin in as them
     """
     sample.sample_data_one_user()
     school_id = School.objects.get().id
     view = UserViewSet.as_view(actions={'post': 'create'})
     email = '*****@*****.**'
     password = '******'
     # form data to submit
     data = {
         'email': email,
         'password': password,
         'school': school_id,
         'first_name': 'first',
         'last_name': 'last',
         'color_pref': 'green'
     }
     request = self.factory.post('/user/', data)
     response = view(request)
     self.assertEqual(response.status_code, 201,
                      "Did not get a 201 HTTP response.")
     # make sure user can login
     out = self.authenticate(email, password)
     self.assertTrue('token' in out.keys())
Пример #3
0
 def test_authenticate_user_invalid_password(self):
     """
     Try logging in with invalid password
     """
     sample.sample_data_one_user()
     out = self.authenticate('*****@*****.**', 'not my password')
     self.assertDictEqual(out, {'error_msg': 'Invalid password'})
Пример #4
0
 def test_authenticate_user_valid(self):
     """
     Try logging in a registered user
     """
     sample.sample_data_one_user()
     out = self.authenticate('*****@*****.**', 'hello!')
     self.assertTrue('token' in out.keys())
Пример #5
0
 def test_change_password(self):
     """
     try to change the password for a user
     """
     email = '*****@*****.**'
     old_pass = '******'
     new_pass = '******'
     # sample data
     sample.sample_data_one_user()
     # check the original login
     out = self.authenticate(email, old_pass)
     self.assertTrue('token' in out)
     # change the password
     view = UserViewSet.as_view(actions={'put': 'change_password'})
     data = {
         'email': email,
         'old_password': old_pass,
         'new_password': new_pass
     }
     request = self.factory.put('/user/change_password/', data)
     response = view(request)
     # Check the response
     self.assertEqual(response.status_code, 201,
                      "Did not get a 201 HTTP response.")
     response.render()
     out = self.authenticate(email, new_pass)
     # check authenticated
     self.assertTrue('token' in out)
Пример #6
0
 def test_get_account_created(self):
     """"
     The account created date should be recent (since it was just created)
     """
     sample.sample_data_one_user()
     me = User.objects.get(email="*****@*****.**")
     now = datetime.datetime.now(datetime.timezone.utc)
     self.assertLess(now - me.account_created, datetime.timedelta(hours=2),
                     "New account has timestamp over two hours ago.")
Пример #7
0
 def test_delete_school_fails(self):
     """
     Deleteing a school for an existing user should cause an error.
     """
     sample.sample_data_one_user()
     uiuc = School.objects.get(name="University of Illinois Urbana-Champaign")
     try:
         uiuc.delete()
         self.assertTrue(False, "Should have thrown ProtectedError - can't delete school that belongs to student.")
     except ProtectedError:
         pass
Пример #8
0
 def create_course_helper(self):
     """
     Helper function that creates a course with basic data
     :return: The course created
     """
     sample.sample_data_one_user()
     data = {
         'name': 'test_name',
         'description': 'test_desc',
     }
     view = CourseViewSet.as_view(actions={'post': 'create'})
     request = self.factory.post('/course/', data)
     response = view(request)
     self.assertEqual(response.status_code, 201, "Did not get a 201 HTTP response.")
     return User.objects.get(id=1), Course.objects.get(name='test_name')
Пример #9
0
 def test_get_submissions_empty(self):
     """
     test get_submissions with no results
     """
     sample.sample_data_one_user()
     view = SubmissionViewSet.as_view(actions={'post': 'get_submissions'})
     data = {'userid': 1}
     request = self.factory.post('/submission/get_submissions/', data)
     response = view(request)
     self.assertEqual(response.status_code, 200,
                      "Did not get a 200 HTTP response.")
     response.render()
     data = json.loads(response.content.decode('utf-8'))
     # should get nothing back
     self.assertEqual(len(data['submissions']), 0,
                      "Should have no submissions.")
Пример #10
0
 def test_authenticate_decode(self):
     """
     authenticate a user, then try to decode the token response
     """
     sample.sample_data_one_user()
     out = self.authenticate('*****@*****.**', 'hello!')
     self.assertTrue('token' in out)
     view = UserViewSet.as_view(actions={'post': 'decode_token'})
     request = self.factory.post('/user/decode_token/', out)
     response = view(request)
     self.assertEqual(response.status_code, 200,
                      "Did not get a 200 HTTP response.")
     response.render()
     data = json.loads(response.content.decode('utf-8'))
     # should not get error
     self.assertTrue('error_msg' not in data)
Пример #11
0
 def test_create(self):
     """
     Try creating a new course, and check that the access codes are also made.
     """
     sample.sample_data_one_user()
     data = {
         'name': 'test_name',
         'description': 'test_desc',
     }
     view = CourseViewSet.as_view(actions={'post': 'create'})
     request = self.factory.post('/course/', data)
     response = view(request)
     self.assertEqual(response.status_code, 201,
                      "Did not get a 201 HTTP response.")
     response.render()
     course = Course.objects.get(name='test_name')
     # check the access codes
     self.check_get_access_codes(course)
Пример #12
0
 def test_create_new_user_invalid(self):
     """
     Create a new user with a name too long, check that 400 is returned
     """
     sample.sample_data_one_user()
     school_id = School.objects.get().id
     view = UserViewSet.as_view(actions={'post': 'create'})
     email = '*****@*****.**'
     password = '******'
     data = {
         'email': email,
         'password': password,
         'school': school_id,
         'first_name': 'too long' * 100,
         'last_name': 'last',
         'color_pref': 'green'
     }
     request = self.factory.post('/user/', data)
     response = view(request)
     # Check 400 response
     self.assertEqual(response.status_code, 400,
                      "Should get invalid response")