Skip to content

Commit

Permalink
Feat: Remove email test as user creation does not need emails
Browse files Browse the repository at this point in the history
  • Loading branch information
SverreNystad committed Jan 5, 2024
1 parent aa3794a commit 32385f0
Showing 1 changed file with 16 additions and 41 deletions.
57 changes: 16 additions & 41 deletions backend/users/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
from rest_framework import status
import json
from users.models import User

# Create your tests here.

base = "/api/"


class UserCreationTests(TestCase):

def setUp(self):
# This code will run before each test
self.register_end_point = f"{base}create-user/"
self.user = User.objects.create_user(
username='testuser', password='12345')
self.user = User.objects.create_user(username="testuser", password="12345")
self.valid_username = "username"

def tearDown(self):
Expand All @@ -29,7 +28,7 @@ def test_valid_nonexistent_user(self):
"email": "[email protected]",
"first_name": "simon",
"last_name": "sandvik",
"streak_count": 1
"streak_count": 1,
}
response = client.post(self.register_end_point, request_body)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)
Expand All @@ -47,7 +46,7 @@ def test_username_blank(self):
"email": "[email protected]",
"first_name": "simon",
"last_name": "sandvik",
"streak_count": 1
"streak_count": 1,
}
response = client.post(self.register_end_point, request_body)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Expand All @@ -62,7 +61,7 @@ def test_username_too_long(self):
"email": "[email protected]",
"first_name": "simon",
"last_name": "sandvik",
"streak_count": 1
"streak_count": 1,
}
response = client.post(self.register_end_point, request_body)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)
Expand All @@ -75,7 +74,7 @@ def test_register_already_existing_user(self):
"email": "[email protected]",
"first_name": "simon",
"last_name": "sandvik",
"streak_count": 1
"streak_count": 1,
}

response = client.post(self.register_end_point, request_body)
Expand All @@ -90,36 +89,21 @@ def test_valid_email(self):
"email": valid_email,
"first_name": "simon",
"last_name": "sandvik",
"streak_count": 1
"streak_count": 1,
}

response = client.post(self.register_end_point, request_body)
self.assertEqual(response.status_code, status.HTTP_201_CREATED)

def test_invalid_email(self):
client = Client()
invalid_email: str = "testtest.com"
request_body = {
"username": "username",
"password": "test",
"email": invalid_email,
"first_name": "simon",
"last_name": "sandvik",
"streak_count": 1
}

response = client.post(self.register_end_point, request_body)
self.assertEqual(response.status_code, status.HTTP_400_BAD_REQUEST)


class LoginTests(TestCase):

def setUp(self):
self.login_end_point = f"{base}login/"
# This code will run before each test
self.user_pasword = 'a12345'
self.user_pasword = "a12345"
self.user = User.objects.create_user(
username='logintestuser', password=self.user_pasword)
username="logintestuser", password=self.user_pasword
)

def tearDown(self):
# This code will run after each test
Expand All @@ -146,7 +130,7 @@ def test_invalid_login_attempt_to_non_existing_valid_user(self):
invalid_password: str = "P21241!,.$"
request_body: dict = {
"username": invalid_username,
"password": invalid_password
"password": invalid_password,
}

response = client.post(self.login_end_point, request_body)
Expand All @@ -157,7 +141,7 @@ def test_invalid_login_on_existing_user_with_capital_letters_in_username(self):
client = Client()
request_body = {
"username": str(self.user.username).upper(),
"password": self.user_pasword
"password": self.user_pasword,
}

response = client.post(self.login_end_point, request_body)
Expand All @@ -167,38 +151,29 @@ def test_invalid_login_on_existing_user_with_capital_letters_in_password(self):
client = Client()
request_body = {
"username": self.user.username,
"password": str(self.user_pasword).upper()
"password": str(self.user_pasword).upper(),
}

response = client.post(self.login_end_point, request_body)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

def test_valid_login_on_existing_user(self):
client = Client()
request_body = {
"username": self.user.username,
"password": self.user_pasword
}
request_body = {"username": self.user.username, "password": self.user_pasword}

response = client.post(self.login_end_point, request_body)
self.assertEqual(response.status_code, status.HTTP_200_OK)

def test_correct_username_valid_on_existing_user(self):
client = Client()
request_body = {
"username": self.user.username,
"password": "WRONG-PASSWORD"
}
request_body = {"username": self.user.username, "password": "WRONG-PASSWORD"}

response = client.post(self.login_end_point, request_body)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

def test_correct_pass_valid_on_existing_user(self):
client = Client()
request_body = {
"username": "WRONG-PASSWORD",
"password": self.user_pasword
}
request_body = {"username": "WRONG-PASSWORD", "password": self.user_pasword}

response = client.post(self.login_end_point, request_body)
self.assertEqual(response.status_code, status.HTTP_401_UNAUTHORIZED)

0 comments on commit 32385f0

Please sign in to comment.