def test_bad_emails(self): """ If we pass a bad email, get an error about it """ bad_email = 'invalid@spaced domain.com' email_result = {} try: validate_email(bad_email) # validate and get info self.fail('Validate email did not raise exception, fix the test') except EmailNotValidError as e: # email is not valid, exception message is human-readable email_result = error_dict('api_errors', 'email invalid: %s' % e) self.request.json_body = deepcopy(self.good_dict) self.request.json_body['email'] = bad_email result = user_id_put_view(self.request)['d'] self.assertEqual(result, email_result) self.assertNotEqual(result, {}) self.request.json_body = deepcopy(self.good_dict) self.request.json_body['email'] = 234234 # Not a string result = user_id_put_view(self.request)['d'] self.assertEqual( result, error_dict('api_errors', 'email invalid: must be a string'))
def test_not_logged_in(self): """ If we aren't logged in, get an api error """ self.request.user = None result = user_id_put_view(self.request)['d'] self.assertEqual(result, error_dict('api_errors', 'not authenticated for this request'))
def test_wrong_id(self): """ If we don't use the right id in the url, get an api error """ self.request.matchdict = {'user_id': int(self.request.user.id)+4} self.request.json_body = {} result = user_id_put_view(self.request)['d'] self.assertEqual(result, error_dict('api_errors', 'not authenticated for this request'))
def test_bad_password_type(self): """ If we pass in anything but a string, get an error indicating so """ for val in [x for x in bad_data_typevals_list if not isinstance(x, basestring) and x is not None]: self.request.json_body = deepcopy(self.good_dict) self.request.json_body['password'] = val result = user_id_put_view(self.request)['d'] self.assertEqual(result, error_dict('api_errors', 'password must be a string'))
def test_good_data(self): """ If we pass good data, get "OK" and see the stuff changed """ self.assertNotEqual(self.request.user.email, self.good_dict['email']) self.request.json_body = deepcopy(self.good_dict) result = user_id_put_view(self.request)['d'] self.assertEqual(result, dict_from_row(self.request.user, remove_fields=removals)) self.assertEqual(self.request.user.email, self.good_dict['email'])
def test_invalid_password(self): """ If we give a string of insufficient complexity, error """ self.request.json_body = deepcopy(self.good_dict) invalids = ['5horT'] for val in invalids: self.request.json_body['password'] = val result = user_id_put_view(self.request)['d'] self.assertEqual(result, error_dict('api_errors', 'password must be at least 8 characters'))
def test_valid_password(self): """ When we match the appropriate guidelines, the password should be changed """ newpass = '******' m = hashlib.sha512() m.update(newpass.encode('utf-8')) m.update(self.request.user.salt) hashed = m.digest() self.request.json_body = deepcopy(self.good_dict) self.assertNotEqual(self.request.user.password, hashed) self.request.json_body['password'] = newpass result = user_id_put_view(self.request)['d'] self.assertEqual(result, dict_from_row(self.request.user, remove_fields=removals)) self.assertEqual(self.request.user.password, hashed)