Skip to content

Commit

Permalink
Add tests for OPTIONS method in users and teams modules (#34)
Browse files Browse the repository at this point in the history
  • Loading branch information
khorolets authored and frol committed Sep 30, 2016
1 parent de01c7a commit 64bc2ad
Show file tree
Hide file tree
Showing 3 changed files with 74 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()
31 changes: 31 additions & 0 deletions tests/modules/teams/resources/test_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# 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,expected_allowed_methods', (
('/api/v1/teams/', {'GET', 'POST', 'OPTIONS'}),
('/api/v1/teams/1', {'GET', 'OPTIONS', 'PATCH', 'DELETE'}),
('/api/v1/teams/2', {'OPTIONS'}),
))
def test_teams_options_authorized(
path,
expected_allowed_methods,
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 set(response.headers['Allow'].split(', ')) == expected_allowed_methods
26 changes: 26 additions & 0 deletions tests/modules/users/resources/test_options.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# pylint: disable=missing-docstring
import pytest

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

assert response.status_code == status_code
if expected_allowed_methods:
assert set(response.headers['Allow'].split(', ')) == expected_allowed_methods


@pytest.mark.parametrize('path,expected_allowed_methods', (
('/api/v1/users/', {'POST', 'OPTIONS'}),
('/api/v1/users/1', {'GET', 'OPTIONS', 'PATCH'}),
('/api/v1/users/2', {'OPTIONS'}),
))
def test_users_options_authorized(path, expected_allowed_methods, 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 set(response.headers['Allow'].split(', ')) == expected_allowed_methods

0 comments on commit 64bc2ad

Please sign in to comment.