def test_edit_profile_req_bool_no_change(self): custom_field = CustomField(id='bool_req', label='Boolean required', required=True, type='bool') custom_field.save() upcf = UserProfileCustomField(key_name=custom_field, user=self.user, value_bool=True) upcf.save() post_data = self.base_data.copy() post_data['bool_req'] = True count_start = UserProfileCustomField.objects.all().count() response = self.api_client.post(self.url, format='json', data=post_data, authentication=self.get_credentials()) self.assertHttpCreated(response) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start, count_end) updated_row = UserProfileCustomField.objects.get(key_name=custom_field, user=self.user) self.assertEqual(updated_row.value_bool, True)
def test_edit_profile_req_bool_no_change(self): custom_field = CustomField(id='bool_req', label='Boolean required', required=True, type='bool') custom_field.save() upcf = UserProfileCustomField(key_name=custom_field, user=self.normal_user, value_bool=True) upcf.save() self.client.force_login(self.normal_user) post_data = { 'organisation': self.normal_user.userprofile.organisation, 'email': self.normal_user.email, 'username': self.normal_user.username, 'first_name': self.normal_user.first_name, 'last_name': self.normal_user.last_name, 'bool_req': True } count_start = UserProfileCustomField.objects.all().count() response = self.client.post(self.url, data=post_data) self.assertEqual(response.status_code, 200) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start, count_end) updated_row = UserProfileCustomField.objects.get(key_name=custom_field, user=self.normal_user) self.assertEqual(updated_row.value_bool, True)
def test_edit_profile_not_req_str_change(self): custom_field = CustomField(id='str_not_req', label='String not required', required=False, type='str') custom_field.save() upcf = UserProfileCustomField(key_name=custom_field, user=self.normal_user, value_str="my string") upcf.save() self.client.force_login(self.normal_user) post_data = { 'organisation': self.normal_user.userprofile.organisation, 'email': self.normal_user.email, 'username': self.normal_user.username, 'first_name': self.normal_user.first_name, 'last_name': self.normal_user.last_name, 'str_not_req': "my new string" } count_start = UserProfileCustomField.objects.all().count() response = self.client.post(self.url, data=post_data) self.assertEqual(response.status_code, 200) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start, count_end) updated_row = UserProfileCustomField.objects.get(key_name=custom_field, user=self.normal_user) self.assertEqual(updated_row.value_str, "my new string")
def test_custom_field_model_name(self): custom_field = CustomField(id='my_cf_key', label='String', required=True, type='str') custom_field.save() self.assertEqual(str(custom_field), 'my_cf_key')
def test_wrong_type_str_in_bool(self): custom_field = CustomField(id='bool', label='Boolean', required=True, type='bool') custom_field.save() with self.assertRaises(ValidationError): UserProfileCustomField(key_name=custom_field, user=self.normal_user, value_bool=self.VALUE_STR_DEFAULT).save()
def test_wrong_type_str_in_int(self): custom_field = CustomField(id='int', label='Integer', required=True, type='int') custom_field.save() with self.assertRaises(ValueError): UserProfileCustomField(key_name=custom_field, user=self.normal_user, value_int=self.VALUE_STR_DEFAULT).save()
def test_wrong_type_int_in_str(self): custom_field = CustomField(id='str', label='String', required=True, type='str') custom_field.save() upcf = UserProfileCustomField(key_name=custom_field, user=self.normal_user, value_str=123) upcf.save() self.assertEqual(123, upcf.get_value())
def test_wrong_type_int_in_bool_1(self): custom_field = CustomField(id='bool', label='Boolean', required=True, type='bool') custom_field.save() upcf = UserProfileCustomField(key_name=custom_field, user=self.normal_user, value_bool=1) upcf.save() self.assertEqual(1, upcf.get_value())
def test_user_custom_field_model_name(self): custom_field = CustomField(id='str', label='String', required=True, type='str') custom_field.save() upcf = UserProfileCustomField(key_name=custom_field, user=self.normal_user, value_str=self.VALUE_STR_DEFAULT) upcf.save() self.assertEqual('str: demo', str(upcf))
def test_int_required_field_without_int(self): custom_field = CustomField(id='int_req', label='Integer required', required=True, type='int') custom_field.save() count_start = UserProfileCustomField.objects.all().count() self.client.post(self.url, data=self.base_filled_form) self.assertRaises(ValidationError) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start, count_end)
def test_int_not_required_field_without_int(self): custom_field = CustomField(id='int_not_req', label='Integer not required', required=False, type='int') custom_field.save() count_start = UserProfileCustomField.objects.all().count() resp = self.client.post(self.url, data=self.base_filled_form) self.assertRedirects(resp, expected_url=self.thanks_url) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start, count_end)
def test_wrong_type_bool_in_int(self): custom_field = CustomField(id='int', label='Integer', required=True, type='int') custom_field.save() upcf = UserProfileCustomField(key_name=custom_field, user=self.normal_user, value_int=True) upcf.save() self.assertEqual(True, upcf.get_value()) upcf.value_int = False upcf.save() self.assertEqual(False, upcf.get_value())
def test_str_required_field_without_str(self): custom_field = CustomField(id='str_req', label='String required', required=True, type='str') custom_field.save() count_start = UserProfileCustomField.objects.all().count() resp = self.api_client.post(self.url, format='json', data=self.base_data) self.assertHttpBadRequest(resp) self.assertValidJSON(resp.content) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start, count_end)
def test_edit_profile_no_data(self): custom_field = CustomField(id='bool_req', label='Boolean required', required=True, type='bool') custom_field.save() count_start = UserProfileCustomField.objects.all().count() response = self.api_client.post(self.url, format='json', data=self.base_data, authentication=self.get_credentials()) self.assertHttpBadRequest(response) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start, count_end)
def test_custom_field_get_value_int(self): custom_field = CustomField(id='int', label='Integer', required=True, type='int') custom_field.save() upcf = UserProfileCustomField(key_name=custom_field, user=self.normal_user, value_int=123) upcf.save() self.assertEqual(upcf.get_value(), 123) self.assertNotEqual(upcf.get_value(), "123") self.assertNotEqual(upcf.get_value(), True) self.assertNotEqual(upcf.get_value(), False) self.assertNotEqual(upcf.get_value(), None)
def test_custom_field_multiple_rows(self): custom_field = CustomField(id='str', label='String', required=True, type='str') custom_field.save() upcf = UserProfileCustomField(key_name=custom_field, user=self.normal_user, value_str=self.VALUE_STR_DEFAULT) upcf.save() with self.assertRaises(IntegrityError): upcf = UserProfileCustomField(key_name=custom_field, user=self.normal_user, value_str="my other string") upcf.save()
def test_custom_field_get_value_bool(self): custom_field = CustomField(id='bool', label='Boolean', required=True, type='bool') custom_field.save() upcf = UserProfileCustomField(key_name=custom_field, user=self.normal_user, value_bool=True) upcf.save() self.assertEqual(upcf.get_value(), True) self.assertNotEqual(upcf.get_value(), "True") self.assertNotEqual(upcf.get_value(), 123) self.assertNotEqual(upcf.get_value(), False) self.assertNotEqual(upcf.get_value(), None)
def test_custom_field_get_value_str(self): custom_field = CustomField(id='str', label='String', required=True, type='str') custom_field.save() upcf = UserProfileCustomField(key_name=custom_field, user=self.normal_user, value_str=self.VALUE_STR_DEFAULT) upcf.save() self.assertEqual(upcf.get_value(), self.VALUE_STR_DEFAULT) self.assertNotEqual(upcf.get_value(), True) self.assertNotEqual(upcf.get_value(), False) self.assertNotEqual(upcf.get_value(), None) self.assertNotEqual(upcf.get_value(), 123)
def test_str_required_field_with_str_blank(self): custom_field = CustomField(id='str_req', label='String required', required=True, type='str') custom_field.save() count_start = UserProfileCustomField.objects.all().count() # with int in form - correct filled_form = self.base_filled_form.copy() filled_form['str_req'] = "" count_start = UserProfileCustomField.objects.all().count() self.client.post(self.url, data=filled_form) self.assertRaises(ValidationError) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start, count_end)
def test_str_not_required_field_with_str_blank(self): custom_field = CustomField(id='str_not_req', label='String not required', required=False, type='str') custom_field.save() count_start = UserProfileCustomField.objects.all().count() # with int in form - correct filled_form = self.base_filled_form.copy() filled_form['str_not_req'] = "" resp = self.client.post(self.url, data=filled_form) self.assertRedirects(resp, expected_url=self.thanks_url) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start, count_end)
def test_int_not_required_field_without_int(self): custom_field = CustomField(id='int_not_req', label='Integer not required', required=False, type='int') custom_field.save() count_start = UserProfileCustomField.objects.all().count() resp = self.api_client.post(self.url, format='json', data=self.base_data) self.assertHttpCreated(resp) self.assertValidJSON(resp.content) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start, count_end)
def test_bool_required_field_with_bool_false(self): custom_field = CustomField(id='bool_req', label='Boolean required', required=True, type='bool') custom_field.save() data = self.base_data.copy() data['bool_req'] = False count_start = UserProfileCustomField.objects.all().count() resp = self.api_client.post(self.url, format='json', data=data) self.assertHttpBadRequest(resp) self.assertValidJSON(resp.content) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start, count_end)
def test_str_not_required_field_with_str_blank(self): custom_field = CustomField(id='str_not_req', label='String not required', required=False, type='str') custom_field.save() count_start = UserProfileCustomField.objects.all().count() # with int in form - correct data = self.base_data.copy() data['str_not_req'] = "" resp = self.api_client.post(self.url, format='json', data=data) self.assertHttpCreated(resp) self.assertValidJSON(resp.content) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start, count_end)
def test_edit_profile_req_str_new(self): custom_field = CustomField(id='str_req', label='String required', required=True, type='str') custom_field.save() post_data = self.base_data.copy() post_data['str_req'] = "my new string" count_start = UserProfileCustomField.objects.all().count() response = self.api_client.post(self.url, format='json', data=post_data, authentication=self.get_credentials()) self.assertHttpCreated(response) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start + 1, count_end) updated_row = UserProfileCustomField.objects.get(key_name=custom_field, user=self.user) self.assertEqual(updated_row.value_str, "my new string")
def test_edit_profile_no_data(self): custom_field = CustomField(id='bool_req', label='Boolean required', required=True, type='bool') custom_field.save() self.client.force_login(self.normal_user) post_data = { 'organisation': self.normal_user.userprofile.organisation, 'email': self.normal_user.email, 'username': self.normal_user.username, 'first_name': self.normal_user.first_name, 'last_name': self.normal_user.last_name } count_start = UserProfileCustomField.objects.all().count() self.client.post(self.url, data=post_data) self.assertRaises(ValidationError) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start, count_end)
def test_str_required_field_with_str(self): custom_field = CustomField(id='str_req', label='String required', required=True, type='str') custom_field.save() count_start = UserProfileCustomField.objects.all().count() # with int in form - correct filled_form = self.base_filled_form.copy() filled_form['str_req'] = "my string" resp = self.client.post(self.url, data=filled_form) self.assertRedirects(resp, expected_url=self.thanks_url) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start + 1, count_end) # check saved to value_int field saved_row = UserProfileCustomField.objects.latest('created') self.assertEqual(saved_row.value_str, "my string")
def test_int_not_required_field_with_int(self): custom_field = CustomField(id='int_not_req', label='Integer not required', required=False, type='int') custom_field.save() count_start = UserProfileCustomField.objects.all().count() # with int in form - correct filled_form = self.base_filled_form.copy() filled_form['int_not_req'] = 123 resp = self.client.post(self.url, data=filled_form) self.assertRedirects(resp, expected_url=self.thanks_url) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start + 1, count_end) # check saved to value_int field saved_row = UserProfileCustomField.objects.latest('created') self.assertEqual(saved_row.value_int, 123)
def test_str_required_field_with_str(self): custom_field = CustomField(id='str_req', label='String required', required=True, type='str') custom_field.save() count_start = UserProfileCustomField.objects.all().count() # with int in form - correct data = self.base_data.copy() data['str_req'] = "my string" resp = self.api_client.post(self.url, format='json', data=data) self.assertHttpCreated(resp) self.assertValidJSON(resp.content) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start + 1, count_end) # check saved to value_int field saved_row = UserProfileCustomField.objects.latest('created') self.assertEqual(saved_row.value_str, "my string")
def test_bool_not_required_field_with_bool_false(self): custom_field = CustomField(id='bool_not_req', label='Boolean not required', required=False, type='bool') custom_field.save() count_start = UserProfileCustomField.objects.all().count() # with int in form - correct data = self.base_data.copy() data['bool_not_req'] = False resp = self.api_client.post(self.url, format='json', data=data) self.assertHttpCreated(resp) self.assertValidJSON(resp.content) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start + 1, count_end) # check saved to value_bool field saved_row = UserProfileCustomField.objects.latest('created') self.assertEqual(saved_row.value_bool, False)
def test_edit_profile_req_int_new(self): custom_field = CustomField(id='int_req', label='Integer required', required=True, type='int') custom_field.save() self.client.force_login(self.normal_user) post_data = { 'organisation': self.normal_user.userprofile.organisation, 'email': self.normal_user.email, 'username': self.normal_user.username, 'first_name': self.normal_user.first_name, 'last_name': self.normal_user.last_name, 'int_req': 123 } count_start = UserProfileCustomField.objects.all().count() response = self.client.post(self.url, data=post_data) self.assertEqual(response.status_code, 200) count_end = UserProfileCustomField.objects.all().count() self.assertEqual(count_start + 1, count_end) updated_row = UserProfileCustomField.objects.get(key_name=custom_field, user=self.normal_user) self.assertEqual(updated_row.value_int, 123)