Skip to content

Commit

Permalink
Add tests for OPTIONS method in users and teams modules
Browse files Browse the repository at this point in the history
  • Loading branch information
khorolets committed Sep 30, 2016
1 parent de01c7a commit 7ca37ae
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/modules/teams/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,20 @@ def team_for_regular_user(db, regular_user, readonly_user):
db.session.delete(regular_user_team_member)
db.session.delete(team)
db.session.commit()

@pytest.yield_fixture()
def team_for_nobody(db): # pylint: disable=invalid-name
"""
Create a team that not belongs regural user
"""
from app.modules.teams.models import Team

team = Team(title="Admin User's team")
db.session.add(team)
db.session.commit()

yield team

# Cleanup
db.session.delete(team)
db.session.commit()
33 changes: 33 additions & 0 deletions tests/modules/teams/resources/test_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# pylint: disable=missing-docstring
import pytest

@pytest.mark.parametrize('path,status_code', (
('/api/v1/teams/', 401),
('/api/v1/users/1', 401),
))
def test_teams_options_unauthorized(path, status_code, flask_app_client):
response = flask_app_client.options(path)

assert response.status_code == status_code


@pytest.mark.parametrize('path,result', (
('/api/v1/teams/', ['POST', 'OPTIONS']),
('/api/v1/teams/1', ['GET', 'OPTIONS', 'PATCH']),
('/api/v1/teams/2', ['OPTIONS']),
))
def test_teams_options_authorized(
path,
result,
flask_app_client,
regular_user,
team_for_regular_user,
team_for_nobody
):
with flask_app_client.login(regular_user, auth_scopes=('teams:write', 'teams:read')):
response = flask_app_client.options(path)

assert response.status_code == 204
assert len(
list(set(result) - set(response.headers['Allow'].replace(' ', '').split(',')))
) == 0
30 changes: 30 additions & 0 deletions tests/modules/users/resources/test_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# pylint: disable=missing-docstring
import pytest

@pytest.mark.parametrize('path,status_code,result', (
('/api/v1/users/', 204, ['POST', 'OPTIONS']),
('/api/v1/users/1', 401, None),
))
def test_users_options_unauthorized(path, status_code, result, flask_app_client):
response = flask_app_client.options(path)

assert response.status_code == status_code
if result:
assert len(
list(set(result) - set(response.headers['Allow'].replace(' ', '').split(',')))
) == 0


@pytest.mark.parametrize('path,result', (
('/api/v1/users/', ['POST', 'OPTIONS']),
('/api/v1/users/1', ['GET', 'OPTIONS', 'PATCH']),
('/api/v1/users/2', ['OPTIONS']),
))
def test_users_options_authorized(path, result, flask_app_client, regular_user):
with flask_app_client.login(regular_user, auth_scopes=('users:write', 'users:read')):
response = flask_app_client.options(path)

assert response.status_code == 204
assert len(
list(set(result) - set(response.headers['Allow'].replace(' ', '').split(',')))
) == 0

0 comments on commit 7ca37ae

Please sign in to comment.