Auth Module#

The Auth module is responsible for various functionalities. This section documents the components of the Booth module, including models, views, and more.

views.py#

class authentication.views.CustomUserCreationForm[source]#

Bases: UserCreationForm

Custom form for user registration.

Extends UserCreationForm to add custom validation methods for username and password.

class Meta[source]#

Bases: Meta

fields = ('username', 'password1', 'password2', 'email', 'first_name', 'last_name')#
labels = {'email': 'Email', 'first_name': 'First Name', 'last_name': 'Last Name', 'password1': 'Password', 'password2': 'Confirm Password', 'username': 'Username'}#
model#

alias of User

base_fields = {'email': <django.forms.fields.EmailField object>, 'first_name': <django.forms.fields.CharField object>, 'last_name': <django.forms.fields.CharField object>, 'password1': <django.forms.fields.CharField object>, 'password2': <django.forms.fields.CharField object>, 'username': <django.contrib.auth.forms.UsernameField object>}#
clean_confirmation(password, confirm_password)[source]#

Validate that the entered passwords match.

Parameters:
  • password (str) – The first entered password.

  • confirm_password (str) – The second entered password for confirmation.

Returns:

True if the passwords do not match, False otherwise.

Return type:

bool

clean_password_common(password)[source]#

Check if the entered password is common.

Parameters:

password (str) – The password to be checked.

Returns:

True if the password is common, False otherwise.

Return type:

bool

clean_password_lenght(password)[source]#

Validate the length of the password.

Parameters:

password (str) – The password to be validated.

Returns:

True if the password has fewer than 8 characters, False otherwise.

Return type:

bool

clean_password_numeric(password)[source]#

Check if the password consists only of numeric characters.

Parameters:

password (str) – The password to be checked.

Returns:

True if the password consists only of numeric characters, False otherwise.

Return type:

bool

clean_password_too_similar(password, username, first_name, last_name)[source]#

Check if the password is too similar to personal data.

Parameters:
  • password (str) – The password to be checked.

  • username (str) – The username of the user.

  • first_name (str) – The first name of the user.

  • last_name (str) – The last name of the user.

Returns:

True if the password is too similar to personal data, False otherwise.

Return type:

bool

declared_fields = {'password1': <django.forms.fields.CharField object>, 'password2': <django.forms.fields.CharField object>}#
email_clean(email)[source]#

Check if the email already exists in the database.

Parameters:

email (str) – The email to be checked.

Returns:

True if the email already exists, False otherwise.

Return type:

bool

property media#

Return all media required to render the widgets on this form.

username_clean_exits(username)[source]#

Check if the username already exists in the database.

Parameters:

username (str) – The username to be checked.

Returns:

True if the username already exists, False otherwise.

Return type:

bool

username_clean_lenght(username)[source]#

Validate the length of the username.

Parameters:

username (str) – The username to be validated.

Returns:

True if the username is longer than 150 characters, False otherwise.

Return type:

bool

username_clean_pattern(username)[source]#

Validate the username against a specific pattern.

Parameters:

username (str) – The username to be validated.

Returns:

True if the username does not match the pattern, False otherwise.

Return type:

bool

class authentication.views.GetUserView[source]#

Bases: APIView

API view for retrieving user information.

Handles POST requests to retrieve user information based on the provided user token.

post(request)[source]#

Handle POST requests to retrieve user information.

Parameters:

request (Request) – The incoming HTTP request containing the user token.

Returns:

An HTTP response containing the serialized user data.

Return type:

Response

class authentication.views.LoginView[source]#

Bases: CreateView

View for handling user login.

Inherits from CreateView to handle user login. Uses CustomUserCreationForm for customized user registration.

form_class#

alias of CustomUserCreationForm

model#

alias of User

post(request)[source]#

Handle POST requests to log in a user.

Parameters:

request (Request) – The incoming HTTP request containing the username and password.

Returns:

An HTTP response indicating the success or failure of the login operation.

Return type:

HttpResponse

template_name = 'authentication/login.html'#
class authentication.views.LogoutView[source]#

Bases: APIView

API view for logging out a user.

Handles POST requests to log out a user by deleting their authentication token.

post(request)[source]#

Handle POST requests to log out a user.

Parameters:

request (Request) – The incoming HTTP request containing the user token.

Returns:

An HTTP response indicating the success or failure of the logout operation.

Return type:

Response

class authentication.views.RegisterView[source]#

Bases: CreateView

View for user registration.

Inherits from CreateView to handle user registration. Uses CustomUserCreationForm for customized user registration.

form_class#

alias of CustomUserCreationForm

get_form(form_class=None)[source]#

Customize the form widget attributes.

Parameters:

form_class (CustomUserCreationForm) – The form class.

Returns:

The customized form.

Return type:

CustomUserCreationForm

model#

alias of User

post(request)[source]#

Handle POST requests to register a new user.

Parameters:

request (Request) – The incoming HTTP request containing user registration data.

Returns:

An HTTP response indicating the success or failure of the registration.

Return type:

HttpResponse

template_name = 'authentication/register.html'#
class authentication.views.RegisterViewAPI[source]#

Bases: APIView

API view for registering a new user.

Handles POST requests to register a new user with admin privileges.

post(request)[source]#

Handle POST requests to register a new user.

Parameters:

request (Request) – The incoming HTTP request containing the admin token, username, and password.

Returns:

An HTTP response indicating the success or failure of the registration.

Return type:

Response

authentication.views.logout_view(request)[source]#

Logout view to handle user logout.

Parameters:

request (Request) – The incoming HTTP request.

Returns:

An HTTP response redirecting to the home page after logout.

Return type:

HttpResponse

authentication.views.main(request)[source]#

Main view for handling authentication.

Parameters:

request (Request) – The incoming HTTP request.

Returns:

An HTTP response with the authentication template.

Return type:

HttpResponse

serializers.py#

class authentication.serializers.UserSerializer[source]#

Bases: HyperlinkedModelSerializer

Serializer class for User model.

Serializes User instances to JSON data and vice versa.

model#

The User model to be serialized.

fields#

The fields to include in the serialized data.

class Meta[source]#

Bases: object

Meta class for UserSerializer.

Defines metadata options for the serializer.

model#

The User model to be serialized.

fields#

The fields to include in the serialized data.

fields = ('id', 'username', 'first_name', 'last_name', 'email', 'is_staff')#
model#

alias of User

test_selenium.py#

class authentication.test_selenium.TestLoginNegative[source]#

Bases: StaticLiveServerTestCase

Test case for negative user login scenarios.

Inherits from StaticLiveServerTestCase to test views using a live server.

Methods: - setUp: Set up the test environment before each test case. - tearDown: Tear down the test environment after each test case. - testloginnegative: Test negative user login.

setUp()[source]#

Set up the test environment before each test case.

  • Creates a BaseTestCase instance.

  • Configures a headless Chrome browser for testing.

tearDown()[source]#

Tear down the test environment after each test case.

  • Quits the Chrome browser.

  • Calls the tearDown method of the BaseTestCase instance.

testloginnegative()[source]#

Test negative user login.

  • Accesses the login view.

  • Attempts to log in with invalid credentials.

  • Asserts that the user stays on the login view and sees an alert.

class authentication.test_selenium.TestLoginPositive[source]#

Bases: StaticLiveServerTestCase

Test case for positive user login scenarios.

Inherits from StaticLiveServerTestCase to test views using a live server.

Methods: - setUp: Set up the test environment before each test case. - tearDown: Tear down the test environment after each test case. - testloginpositive: Test positive user login.

setUp()[source]#

Set up the test environment before each test case.

  • Creates a BaseTestCase instance.

  • Configures a headless Chrome browser for testing.

tearDown()[source]#

Tear down the test environment after each test case.

  • Quits the Chrome browser.

  • Calls the tearDown method of the BaseTestCase instance.

testloginpositive()[source]#

Test positive user login.

  • Accesses the registration view.

  • Registers a new user.

  • Logs in with the registered user credentials.

  • Asserts that the user is redirected to the home page.

class authentication.test_selenium.TestRegisterNegative[source]#

Bases: StaticLiveServerTestCase

Test case for negative user registration scenarios.

Inherits from StaticLiveServerTestCase to test views using a live server.

Methods: - setUp: Set up the test environment before each test case. - tearDown: Tear down the test environment after each test case. - testregisternegativewrongpassword: Test user registration with mismatched passwords. - testregisternegativelongusername: Test user registration with a too-long username. - testregisternegativeusername: Test user registration with an already taken username. - testregisternegativepatternusername: Test user registration with an invalid username pattern. - testregisternegativeemail: Test user registration with an already taken email. - testregisternegativeemail: Test user registration with an invalid email. - testregisternegativeemail: Test user registration with a short password. - testregisternegativecommonpass: Test user registration with a common password. - testregisternegativesimilarpass: Test user registration with a password similar to the username. - testregisternegativenumericpass: Test user registration with a numeric password.

setUp()[source]#

Set up the test environment before each test case.

  • Creates a BaseTestCase instance.

  • Configures a headless Chrome browser for testing.

  • Sets up a mock API client.

  • Creates a test user in the database.

tearDown()[source]#

Tear down the test environment after each test case.

  • Quits the Chrome browser.

  • Calls the tearDown method of the BaseTestCase instance.

testregisternegativecommonpass()[source]#

Test user registration with a common password.

  • Accesses the registration view.

  • Fills in the registration form with a common password.

  • Submits the form.

  • Asserts that the user stays on the registration view and sees an alert.

Returns:

None

testregisternegativeemail()[source]#

Test user registration with an already taken email.

  • Accesses the registration view.

  • Fills in the registration form with an already taken email.

  • Submits the form.

  • Asserts that the user stays on the registration view and sees an alert.

Returns:

None

testregisternegativelongusername()[source]#

Test user registration with a too-long username.

  • Accesses the registration view.

  • Fills in the registration form with a too-long username.

  • Submits the form.

  • Asserts that the user stays on the registration view and sees an alert.

Returns:

None

testregisternegativenumericpass()[source]#

Test user registration with a numeric password.

  • Accesses the registration view.

  • Fills in the registration form with a numeric password.

  • Submits the form.

  • Asserts that the user stays on the registration view and sees an alert.

Returns:

None

testregisternegativepatternusername()[source]#

Test user registration with an invalid username pattern.

  • Accesses the registration view.

  • Fills in the registration form with an invalid username pattern.

  • Submits the form.

  • Asserts that the user stays on the registration view and sees an alert.

Returns:

None

testregisternegativesimilarpass()[source]#

Test user registration with a password similar to the username.

  • Accesses the registration view.

  • Fills in the registration form with a password similar to the username.

  • Submits the form.

  • Asserts that the user stays on the registration view and sees an alert.

Returns:

None

testregisternegativeusername()[source]#

Test user registration with an already taken username.

  • Accesses the registration view.

  • Fills in the registration form with an already taken username.

  • Submits the form.

  • Asserts that the user stays on the registration view and sees an alert.

Returns:

None

testregisternegativewrongpassword()[source]#

Test user registration with mismatched passwords.

  • Accesses the registration view.

  • Fills in registration form with mismatched passwords.

  • Submits the form.

  • Asserts that the user stays on the registration view and sees an alert.

class authentication.test_selenium.TestRegisterPositive[source]#

Bases: StaticLiveServerTestCase

Test case for positive user registration scenarios.

Inherits from StaticLiveServerTestCase to test views using a live server.

Methods: - setUp: Set up the test environment before each test case. - tearDown: Tear down the test environment after each test case. - testregisterpositive: Test positive user registration.

setUp()[source]#

Set up the test environment before each test case.

  • Creates a BaseTestCase instance.

  • Configures a headless Chrome browser for testing.

tearDown()[source]#

Tear down the test environment after each test case.

  • Quits the Chrome browser.

  • Calls the tearDown method of the BaseTestCase instance.

testregisterpositive()[source]#

Test positive user registration.

  • Accesses the registration view.

  • Fills in valid user registration information.

  • Submits the registration form.

  • Asserts that the user is redirected to the home page.

tests.py#

class authentication.tests.AuthTestCase[source]#

Bases: APITestCase

Test case for authentication-related functionality.

Inherits from APITestCase to provide utility functions for making API requests.

setUp()[source]#

Set up the test environment.

Creates a test client, mocks a database query, and creates two users for testing.

tearDown()[source]#

Tear down the test environment.

Resets the test client to None.

test_getuser()[source]#

Test retrieving user information after successful login.

Logs in a user, retrieves the user information, and checks if the received data is correct.

Returns:

None

test_getuser_invalid_token()[source]#

Test retrieving user information with an invalid token.

Logs in a user, logs them out, and then attempts to retrieve user information with the invalidated token. Expects a 404 status code.

Returns:

None

test_getuser_invented_token()[source]#

Test retrieving user information with an invented token.

Tries to retrieve user information with a token that does not exist and expects a 404 status code.

Returns:

None

test_login()[source]#

Test the login functionality.

Attempts to log in with valid credentials and checks for the presence of a token.

Returns:

None

test_login_fail()[source]#

Test login failure with incorrect password.

Attempts to log in with incorrect credentials and expects a 400 status code.

Returns:

None

test_logout()[source]#

Test user logout.

Logs in a user, logs them out, and checks if the corresponding token is removed.

Returns:

None

test_register()[source]#

Test user registration.

Logs in as an admin, attempts to register a new user, and checks if the registration is successful.

Returns:

None

test_register_bad_permissions()[source]#

Test user registration with insufficient permissions.

Logs in a user with insufficient permissions, attempts to register a new user, and expects a 401 status code.

Returns:

None

test_register_bad_request()[source]#

Test user registration with a bad request.

Logs in as an admin, attempts to register a new user with incomplete data, and expects a 400 status code.

Returns:

None

test_register_user_already_exist()[source]#

Test user registration when the user already exists.

Logs in as an admin, attempts to register an existing user, and expects a 400 status code.

Returns:

None