Skip to content

Instantly share code, notes, and snippets.

@NicholusMuwonge
Created April 5, 2019 13:39
Show Gist options
  • Save NicholusMuwonge/33e85a39d93085de808ebe0b509c7391 to your computer and use it in GitHub Desktop.
Save NicholusMuwonge/33e85a39d93085de808ebe0b509c7391 to your computer and use it in GitHub Desktop.
class RegistrationAPIViewTestCase(TestCase):
""" This class defines the test suite for the registration view. """
def setUp(self):
self.existing_user_data = {
"username": "janejones",
"email": "jjones@email.com",
"password": "Enter-123"}
self.existing_user = User.objects.create_user(
**self.existing_user_data)
self.client = APIClient()
def test_api_can_create_a_user(self):
user_data = {
"username": "henryjones",
"email":'mwngnchls@gmail.com',
"password": "Enterwithus12349"}
response = self.client.post(
'/api/users/',
{"user": user_data},
format="json")
print("Response ", response.data)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
def test_api_cannot_create_a_user_with_existing_email(self):
user_data = {
"username": "peter",
"email": self.existing_user_data["email"],
"password": "Enter123"}
response = self.client.post(
'/api/users/',
{"user": user_data},
format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn("user with this email already exists.",
response.data["errors"]["email"], )
def test_api_cannot_create_a_user_with_existing_username(self):
user_data = {
"username": self.existing_user_data["username"],
"email": "peter@email.com",
"password": "Enter123"}
response = self.client.post(
'/api/users/',
{"user": user_data},
format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn("user with this username already exists.",
response.data["errors"]["username"])
def test_api_cannot_create_a_user_with_password_lessthan_eight_characters(self):
user_data = {
"username": "peter",
"email": "peter@email.com",
"password": "Enter"}
response = self.client.post(
'/api/users/',
{"user": user_data},
format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn("Ensure this field has at least 8 characters.",
response.data["errors"]["password"])
def test_api_cannot_create_user_without_alphanumeric_password(self):
user_data = {
"username": "peter",
"email": "peter@email.com",
"password": "Password"
}
response = self.client.post(
'/api/users/',
{ "user": user_data},
format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn("password must contain a numeric character", response.data["errors"]["password"])
def test_api_cannot_create_user_with_invalid_email(self):
user_data = {
"username": "peter",
"email": "peter@.com",
"password": "Password1"
}
response = self.client.post(
'/api/users/',
{ "user": user_data},
format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn("Enter a valid email address.", response.data["errors"]["email"])
def test_api_cannot_create_user_with_short_username(self):
user_data = {
"username": "pet",
"email": "peter@email.com",
"password": "Password8"
}
response = self.client.post(
'/api/users/',
{ "user": user_data},
format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn("username should be atleast 4 characters long and shouldnt contain special characters", response.data["errors"]["username"])
def test_api_cannot_create_user_with_special_character_in_username(self):
user_data = {
"username": "peter.",
"email": "peter@email.com",
"password": "Password9"
}
response = self.client.post(
'/api/users/',
{ "user": user_data},
format="json")
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
self.assertIn("username should be atleast 4 characters long and shouldnt contain special characters", response.data["errors"]["username"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment