Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Authentication page test api #15

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .pylintrc
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ disable=
C0116,
C0115,
E0401,
W0621
W0621,
W0613,
E0401
42 changes: 42 additions & 0 deletions api/endpoint/authentication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import allure
from api.endpoint.base_method import BaseMethod


class Authentication(BaseMethod):

def __init__(self, login, password):
super().__init__()
self.response = None
self.login = login
self.password = password

def authentication(self):
with allure.step('Send a login request'):
self.response = self.post_request_authentication(self.login, self.password)
return self.response

def returned_200(self):
with allure.step('Check status code 200'):
return self.response.status_code == 200

def returned_400(self):
with allure.step('Check status code 400'):
return self.response.status_code == 400

def returned_status_bad_request(self):
with allure.step('Check response status'):
response_data = self.response.json()
status = response_data['status']
return status == 'BAD_REQUEST'

def returned_message_user_not_found(self, email):
with allure.step('Check response message'):
response_data = self.response.json()
message = response_data['message']
return message == f'User not found with email: {email}'

def returned_message_invalid_data_format(self):
with allure.step('Check response message'):
response_data = self.response.json()
message = response_data['message']
return message == 'Bad credentials'
36 changes: 36 additions & 0 deletions api/endpoint/base_method.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import json
import requests
from api import settings


class BaseMethod:

def __init__(self):
self.base_url = settings.BASE_URL
self.register_user = settings.REGISTER_USER
self.authentication_url = settings.AUTHENTICATION_URL

def post_request_create_user(self, email, password):
headers = {
'Content-Type': 'application/json'
}
data = json.dumps({
"email": f"{email}",
"password": f"{password}"
})
request = requests.request('POST', self.base_url + self.register_user, headers=headers,
data=data, timeout=20)
return request

def post_request_authentication(self, email, password):
headers = {
'Content-Type': 'application/json'
}
data = json.dumps({
"userEmail": f"{email}",
"userPassword": f"{password}"
})
request = requests.request(
'POST', self.base_url + self.authentication_url, data=data, headers=headers, timeout=20
)
return request
66 changes: 66 additions & 0 deletions api/endpoint/registration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import allure
from api.endpoint.base_method import BaseMethod


class Registration(BaseMethod):

def __init__(self, email, password):
super().__init__()
self.confirm_response = None
self.response = None
self.email = email
self.password = password

def create_new_user(self):
with allure.step('Send request to create user'):
self.response = self.post_request_create_user(
self.email, self.password
)
return self.response

def returned_200(self):
with allure.step('Check status code 200'):
return self.response.status_code == 200

def returned_text_registration_initiated(self):
with allure.step('Check response text'):
return self.response.text == 'Registration initiated.' \
' Please check your email for further instructions.'

def returned_text_registration_completed(self):
with allure.step('Check response text'):
return self.response.text == 'Registration completed successfully!'

def returned_message_invalid_email_address(self):
with allure.step('Check response message'):
response_data = self.response.json()
message = response_data['message']
return message == 'Invalid email address'

def returned_message_email_must_be_between_8_and_256_characters(self):
with allure.step('Check response message'):
response_data = self.response.json()
message = response_data['message']
return message == 'User email must be between 8 and 256 characters'

def returned_message_user_exists(self):
with allure.step('Check response message'):
response_data = self.response.json()
message = response_data['message']
return message == 'User with this username is already exists.'

def returned_message_user_mot_found(self, email):
with allure.step('Check response message'):
response_data = self.response.json()
message = response_data['message']
return message == f'User not found with email: {email}'

def returned_status_bad_request(self):
with allure.step('Check response status'):
response_data = self.response.json()
status = response_data['status']
return status == 'BAD_REQUEST'

def returned_400(self):
with allure.step('Check status code 400'):
return self.response.status_code == 400
4 changes: 4 additions & 0 deletions api/settings.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
BASE_URL = 'https://test.app.it-roast.com'
REGISTER_USER = '/api/v1/users/register'
AUTHENTICATION_URL = '/api/v1/auth/login'
LOG_OUT = '/api/v1/auth/logout'
83 changes: 83 additions & 0 deletions api/tests/test_authentication.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import pytest
import allure
from api.endpoint.registration import Registration
from api.endpoint.authentication import Authentication
import test_data


@allure.feature('Authentication')
@allure.story('Authentication with valid email and password')
@pytest.mark.parametrize('login', test_data.LOGINS)
@pytest.mark.parametrize('email', [test_data.EMAIL])
def test_authentication_with_valid_email_and_password(
connect_db, email, check_existence_and_delete_email, login
):
register_endpoint = Registration(email, test_data.PASSWORD)
register_endpoint.create_new_user()
auth_endpoint = Authentication(login, test_data.PASSWORD)
auth_endpoint.authentication()
assert auth_endpoint.returned_200()


@allure.feature('Authentication')
@allure.story('Authentication with incorrect login and incorrect password')
@pytest.mark.parametrize('login', [test_data.EMPTY_FIELD, test_data.NON_EXISTENT_EMAIL])
@pytest.mark.parametrize('password', [test_data.EMPTY_FIELD, test_data.NON_EXISTENT_PASSWORD])
@pytest.mark.parametrize('email', [test_data.EMAIL])
def test_authentication_with_incorrect_login_and_incorrect_password(
connect_db, email, check_existence_and_delete_email, login, password
):
register_endpoint = Registration(email, test_data.PASSWORD)
register_endpoint.create_new_user()
auth_endpoint = Authentication(login, password)
auth_endpoint.authentication()
assert auth_endpoint.returned_400()
assert auth_endpoint.returned_message_user_not_found(login)
assert auth_endpoint.returned_status_bad_request()


@allure.feature('Authentication')
@allure.story('Authentication with incorrect email')
@pytest.mark.parametrize('login', [test_data.EMPTY_FIELD, test_data.NON_EXISTENT_EMAIL])
@pytest.mark.parametrize('email', [test_data.EMAIL])
def test_authentication_with_incorrect_email(
connect_db, email, check_existence_and_delete_email, login
):
register_endpoint = Registration(email, test_data.PASSWORD)
register_endpoint.create_new_user()
auth_endpoint = Authentication(login, test_data.PASSWORD)
auth_endpoint.authentication()
assert auth_endpoint.returned_400()
assert auth_endpoint.returned_message_user_not_found(login)
assert auth_endpoint.returned_status_bad_request()


@allure.feature('Authentication')
@allure.story('Authentication with incorrect password')
@pytest.mark.parametrize('password', [test_data.EMPTY_FIELD, test_data.NON_EXISTENT_PASSWORD])
@pytest.mark.parametrize('email', [test_data.EMAIL])
def test_authentication_with_incorrect_password(
connect_db, email, check_existence_and_delete_email, password
):
register_endpoint = Registration(email, test_data.PASSWORD)
register_endpoint.create_new_user()
auth_endpoint = Authentication(email, password)
auth_endpoint.authentication()
assert auth_endpoint.returned_400()
assert auth_endpoint.returned_message_invalid_data_format()
assert auth_endpoint.returned_status_bad_request()


@allure.feature('Authentication')
@allure.story('Authentication with password in login and login in password')
@pytest.mark.parametrize('email', [test_data.EMAIL])
def test_authentication_with_password_in_login_and_login_in_password(
connect_db, email, check_existence_and_delete_email
):
register_endpoint = Registration(email, test_data.PASSWORD)
register_endpoint.create_new_user()
auth_endpoint = Authentication(test_data.PASSWORD, test_data.EMAIL)
auth_endpoint.authentication()
assert auth_endpoint.returned_400()
assert auth_endpoint.returned_message_user_not_found(test_data.PASSWORD)
assert auth_endpoint.returned_status_bad_request()
142 changes: 142 additions & 0 deletions api/tests/test_registration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import pytest
import allure
from api.endpoint.registration import Registration
import test_data


@allure.feature('Registration')
@allure.story('Send a request with a valid email')
@pytest.mark.parametrize('email', test_data.VALID_EMAILS)
def test_check_valid_emails(connect_db, email, check_existence_and_delete_email):
register_endpoint = Registration(email, test_data.PASSWORD)
register_endpoint.create_new_user()
assert register_endpoint.returned_200()
assert register_endpoint.returned_text_registration_initiated()


@allure.feature('Registration')
@allure.story('Send a request with an invalid email address')
@pytest.mark.parametrize('email', test_data.INVALID_EMAILS)
def test_check_invalid_emails(connect_db, email, check_existence_and_delete_email):
register_endpoint = Registration(email, test_data.PASSWORD)
register_endpoint.create_new_user()
assert register_endpoint.returned_400()
assert register_endpoint.returned_message_invalid_email_address()
assert register_endpoint.returned_status_bad_request()


@allure.feature('Registration')
@allure.story('Check username')
@pytest.mark.parametrize('username', test_data.USERNAMES)
@pytest.mark.parametrize('email', [test_data.EMAIL])
def test_username(connect_db, username, check_existence_and_delete_email, email):
register_endpoint = Registration(email, test_data.PASSWORD)
register_endpoint.create_new_user()
assert register_endpoint.returned_200()
assert register_endpoint.returned_text_registration_initiated()


@allure.feature('Registration')
@allure.story('Send a request with an valid password')
@pytest.mark.parametrize('password', test_data.VALID_PASSWORD)
@pytest.mark.parametrize('email', [test_data.EMAIL])
def test_valid_password(connect_db, password, email, check_existence_and_delete_email):
register_endpoint = Registration(email, password)
register_endpoint.create_new_user()
assert register_endpoint.returned_200()
assert register_endpoint.returned_text_registration_initiated()


@allure.feature('Registration')
@allure.story('Send a request with an invalid password')
@pytest.mark.parametrize('password', test_data.INVALID_PASSWORD)
@pytest.mark.parametrize('email', [test_data.EMAIL])
def test_invalid_password(connect_db, password, email, check_existence_and_delete_email):
register_endpoint = Registration(email, password)
register_endpoint.create_new_user()
assert register_endpoint.returned_400()
assert register_endpoint.returned_status_bad_request()


@allure.feature('Registration')
@allure.story('Send a request with empty fields')
def test_registration_with_empty_fields(connect_db):
register_endpoint = Registration(
test_data.EMPTY_FIELD, test_data.EMPTY_FIELD
)
register_endpoint.create_new_user()
assert register_endpoint.returned_400()
assert register_endpoint.returned_status_bad_request()


@allure.feature('Registration')
@allure.story('Send a request with empty username and password')
def test_registration_with_empty_username_and_password(connect_db):
register_endpoint = Registration(
test_data.EMAIL, test_data.EMPTY_FIELD
)
register_endpoint.create_new_user()
assert register_endpoint.returned_400()
assert register_endpoint.returned_status_bad_request()


@allure.feature('Registration')
@allure.story('Send a request with empty email and password')
def test_registration_with_empty_email_and_password(connect_db):
register_endpoint = Registration(
test_data.EMPTY_FIELD, test_data.EMPTY_FIELD
)
register_endpoint.create_new_user()
assert register_endpoint.returned_400()
assert register_endpoint.returned_status_bad_request()


@allure.feature('Registration')
@allure.story('Send a request with empty username and email')
def test_registration_with_empty_username_and_email(connect_db):
register_endpoint = Registration(
test_data.EMPTY_FIELD, test_data.PASSWORD
)
register_endpoint.create_new_user()
assert register_endpoint.returned_400()
assert register_endpoint.returned_status_bad_request()
assert register_endpoint.returned_message_email_must_be_between_8_and_256_characters


@allure.feature('Registration')
@allure.story('Send a request with data existing user')
@pytest.mark.parametrize('email', [test_data.EMAIL])
def test_registration_with_existing_user(connect_db, check_existence_and_delete_email, email):
register_endpoint = Registration(email, test_data.PASSWORD)
register_endpoint.create_new_user()
register_endpoint.create_new_user()
assert register_endpoint.returned_400()
assert register_endpoint.returned_status_bad_request()
assert register_endpoint.returned_message_user_exists()


@allure.feature('Registration')
@allure.story('Send a request confirm registration')
@pytest.mark.parametrize('email', [test_data.EMAIL])
def test_confirm_registration(
connect_db, check_existence_and_delete_email, email, delete_session
):
register_endpoint = Registration(
email, test_data.PASSWORD
)
register_endpoint.create_new_user()
assert register_endpoint.returned_200()
assert register_endpoint.returned_text_registration_completed()


@allure.feature('Registration')
@allure.story('Send a request confirm registration')
@pytest.mark.parametrize('email', [test_data.EMAIL])
def test_confirm_registration_with_invalid_email(
connect_db, check_existence_and_delete_email, email, delete_session
):
register_endpoint = Registration(email, test_data.PASSWORD)
register_endpoint.create_new_user()
assert register_endpoint.returned_400()
assert register_endpoint.returned_status_bad_request()
assert register_endpoint.returned_message_user_mot_found(test_data.NON_EXISTENT_EMAIL)
Loading