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

test: Start integrating pytest #6827

Merged
merged 16 commits into from May 7, 2020
6 changes: 4 additions & 2 deletions .travis.yml
Expand Up @@ -29,8 +29,9 @@ before_script:
- export TEST_DATABASE_URL=postgresql://postgres@localhost:5432/test
- bash scripts/test_multiple_heads.sh

script:
- nosetests tests/ -v --with-coverage
script: |
nosetests tests/ -v --with-coverage -I tests/all/integration/api/helpers/test_auth.py
pytest tests/all/integration/api/helpers/test_auth.py
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not cool

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know another way to do it.
Somehow I have to exclude test_auth from nostests and test it with pytest


after_success:
- 'bash <(curl -s https://codecov.io/bash)'
Expand All @@ -40,3 +41,4 @@ branches:
only:
- master
- development
- pytest
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not cool

2 changes: 2 additions & 0 deletions requirements/tests.txt
Expand Up @@ -2,3 +2,5 @@

coverage~=5.0
dredd_hooks~=0.2
pytest ~= 5.2.1

76 changes: 39 additions & 37 deletions tests/all/integration/api/helpers/test_auth.py
@@ -1,54 +1,56 @@
import unittest

import pytest
from flask_login import login_user, logout_user

from app.api.helpers.auth import AuthManager
from app.models import db
from app.models.user import User
from tests.all.integration.auth_helper import create_user
from tests.all.integration.utils import OpenEventTestCase
from tests.all.integration.setup_database import Setup


@pytest.fixture(scope='module')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

def app():
app = Setup.create_app()
yield app
Setup.drop_db()


class TestAuthentication(OpenEventTestCase):
def test_load_user(self):
"""Method to test the registered user details"""
def test_load_user(app):
"""Method to test the registered user details"""
with app.test_request_context():
user = create_user(email='authtest@gmail.com', password='password')
assert user == db.session.query(User).get(user.id)

with self.app.test_request_context():
user = create_user(email='authtest@gmail.com', password='password')
self.assertEqual(user, db.session.query(User).get(user.id))

def test_verified_user(self):
"""Method to test if user is verified"""
def test_verified_user(app):
"""Method to test if user is verified"""

with self.app.test_request_context():
user = create_user(email='authtest@gmail.com', password='password')
user.is_verified = False
login_user(user)
self.assertEqual(AuthManager.is_verified_user(), False)
with app.test_request_context():
user = create_user(email='authtest@gmail.com', password='password')
user.is_verified = False
login_user(user)
assert AuthManager.is_verified_user() == False

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comparison to False should be 'if cond is False:' or 'if not cond:'


def test_is_accessible(self):
"""Method to test if user is accessible(authenticated)"""

with self.app.test_request_context():
user = create_user(email='test@test.com', password='password')
login_user(user)
logout_user()
self.assertEqual(AuthManager.is_accessible(), False)
def test_is_accessible(app):
"""Method to test if user is accessible(authenticated)"""

def test_check_auth_admin(self):
"""Method to test proper authentication & admin rights for a user"""
with app.test_request_context():
user = create_user(email='test@test.com', password='password')
login_user(user)
logout_user()
assert AuthManager.is_accessible() == False

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comparison to False should be 'if cond is False:' or 'if not cond:'


with self.app.test_request_context():
user = create_user(email='authtest@gmail.com', password='password')
user.is_admin = True
status = AuthManager.check_auth_admin('authtest@gmail.com', 'password')
self.assertEqual(True, status)

user = create_user(email='authtest2@gmail.com', password='password')
user.is_admin = False
status = AuthManager.check_auth_admin('authtest2@gmail.com', 'password')
self.assertEqual(False, status)
def test_check_auth_admin(app):
"""Method to test proper authentication & admin rights for a user"""

with app.test_request_context():
user = create_user(email='authtest1@gmail.com', password='password')
user.is_admin = True
status = AuthManager.check_auth_admin('authtest1@gmail.com', 'password')
assert True == status

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comparison to True should be 'if cond is True:' or 'if cond:'


if __name__ == '__main__':
unittest.main()
user = create_user(email='authtest2@gmail.com', password='password')
user.is_admin = False
status = AuthManager.check_auth_admin('authtest2@gmail.com', 'password')
assert False == status

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comparison to False should be 'if cond is False:' or 'if not cond:'

67 changes: 26 additions & 41 deletions tests/all/unit/api/helpers/test_errors.py
@@ -1,5 +1,3 @@
from unittest import TestCase

from app.api.helpers.errors import (
BadRequestError,
ErrorResponse,
Expand All @@ -10,42 +8,29 @@
)


class TestErrorDetails(TestCase):
"""Test for error responses"""

def test_error_response_dict_details(self):
"""To test details in the form of dict"""

error_response = ErrorResponse(source="test source", detail="test detail")
expected_dict = {
'status': error_response.status,
'source': error_response.source,
'title': error_response.title,
'detail': error_response.detail,
}
self.assertEqual(error_response.to_dict(), expected_dict)

def test_errors(self):
"""Method to test the status code of all errors"""

# Forbidden Error
forbidden_error = ForbiddenError({'source': ''}, 'Super admin access is required')
self.assertEqual(forbidden_error.status, 403)

# Not Found Error
not_found_error = NotFoundError({'source': ''}, 'Object not found.')
self.assertEqual(not_found_error.status, 404)

# Server Error
server_error = ServerError({'source': ''}, 'Internal Server Error')
self.assertEqual(server_error.status, 500)

# UnprocessableEntity Error
unprocessable_entity_error = UnprocessableEntityError(
{'source': ''}, 'Entity cannot be processed'
)
self.assertEqual(unprocessable_entity_error.status, 422)

# Bad Request Error
bad_request_error = BadRequestError({'source': ''}, 'Request cannot be served')
self.assertEqual(bad_request_error.status, 400)
def test_error_response_dict_details():
"""To test details in the form of dict"""
error_response = ErrorResponse(source="test source", detail="test detail")
expected_dict = {
'status': error_response.status,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Black would make changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pre-commit ran black

'source': error_response.source,
'title': error_response.title,
'detail': error_response.detail,
}
assert error_response.to_dict() == expected_dict


def test_errors():
"""Method to test the status code of all errors"""
forbidden_error = ForbiddenError({'source': ''}, 'Super admin access is required')
assert forbidden_error.status == 403
not_found_error = NotFoundError({'source': ''}, 'Object not found.')
assert not_found_error.status == 404
server_error = ServerError({'source': ''}, 'Internal Server Error')
assert server_error.status == 500
unprocessable_entity_error = UnprocessableEntityError(
{'source': ''}, 'Entity cannot be processed'
)
assert unprocessable_entity_error.status == 422
bad_request_error = BadRequestError({'source': ''}, 'Request cannot be served')
assert bad_request_error.status == 400