Skip to content

Commit

Permalink
Updated tests for using a single login
Browse files Browse the repository at this point in the history
Signed-off-by: Cédric Foellmi <[email protected]>
  • Loading branch information
onekiloparsec committed Aug 16, 2020
1 parent 496b890 commit 72f46a4
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 67 deletions.
10 changes: 5 additions & 5 deletions tests/cli/test_activities.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
from arcsecond import cli
from arcsecond.api.constants import ARCSECOND_API_URL_DEV
from arcsecond.api.error import ArcsecondInputValueError
from tests.utils import register_successful_personal_login
from tests.utils import register_successful_login


@httpretty.activate
def test_activities_with_valid_coordinates():
runner = CliRunner()
register_successful_personal_login(runner)
register_successful_login(runner)
site_uuid = str(uuid.uuid4())
coords_ra = 2.33
coords_dec = 4.55
Expand All @@ -38,7 +38,7 @@ def request_callback(request, uri, response_headers):
@httpretty.activate
def test_activities_with_invalid_coordinates():
runner = CliRunner()
register_successful_personal_login(runner)
register_successful_login(runner)
site_uuid = str(uuid.uuid4())
coords_ra = 2.33
coords_dec = 4.55
Expand All @@ -51,7 +51,7 @@ def test_activities_with_invalid_coordinates():
@httpretty.activate
def test_activities_with_invalid_coordinates2():
runner = CliRunner()
register_successful_personal_login(runner)
register_successful_login(runner)
site_uuid = str(uuid.uuid4())
coords_ra = 2.33
coords_dec = 4.55
Expand All @@ -64,7 +64,7 @@ def test_activities_with_invalid_coordinates2():
@httpretty.activate
def test_activities_with_invalid_coordinates3():
runner = CliRunner()
register_successful_personal_login(runner)
register_successful_login(runner)
site_uuid = str(uuid.uuid4())
coords_ra = 2.33
coords = "yoyo,{}".format(coords_ra)
Expand Down
18 changes: 9 additions & 9 deletions tests/cli/test_datasets_organisations.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
from arcsecond.api.error import ArcsecondError
from arcsecond.config import config_file_clear_section

from tests.utils import (register_successful_personal_login,
register_successful_organisation_login,
from tests.utils import (register_successful_login,
register_successful_login,
mock_http_get,
mock_http_post)

Expand All @@ -23,53 +23,53 @@ def tearDown(self):
def test_datasets_list_unlogged(self):
"""As a simple user, I must not be able to access the list of datasets of an organisation."""
runner = CliRunner()
register_successful_personal_login(runner)
register_successful_login(runner)
result = runner.invoke(cli.datasets, ['--organisation', 'saao', '-d'])
assert result.exit_code != 0 and isinstance(result.exception, ArcsecondError)

def test_organisation_GET_datasets_list_logged_but_wrong_organisation(self):
"""No matter role I have, accessing an unknown organisation must fail."""
runner = CliRunner()
register_successful_organisation_login(runner, 'saao', 'superadmin')
register_successful_login(runner, 'saao', 'superadmin')
result = runner.invoke(cli.datasets, ['--organisation', 'dummy', '-d'])
assert result.exit_code != 0 and isinstance(result.exception, ArcsecondError)

def test_organisation_GET_datasets_list_valid_role(self):
"""As a SAAO superadmin, I must be able to access the list of datasets."""
runner = CliRunner()
register_successful_organisation_login(runner, 'saao', 'superadmin')
register_successful_login(runner, 'saao', 'superadmin')
mock_http_get('/saao/datasets/', '[]')
result = runner.invoke(cli.datasets, ['--organisation', 'saao', '-d'])
assert result.exit_code == 0 and not result.exception

def test_organisation_POST_datasets_list_valid_superadmin_role(self):
"""As a SAAO superadmin, I must be able to create a dataset."""
runner = CliRunner()
register_successful_organisation_login(runner, 'saao', 'superadmin')
register_successful_login(runner, 'saao', 'superadmin')
mock_http_post('/saao/datasets/', '[]')
result = runner.invoke(cli.datasets, ['create', '--organisation', 'saao', '-d'])
assert result.exit_code == 0 and not result.exception

def test_organisation_POST_datasets_list_valid_admin_role(self):
"""As a SAAO admin, I must be able to create a dataset."""
runner = CliRunner()
register_successful_organisation_login(runner, 'saao', 'admin')
register_successful_login(runner, 'saao', 'admin')
mock_http_post('/saao/datasets/', '[]')
result = runner.invoke(cli.datasets, ['create', '--organisation', 'saao', '-d'])
assert result.exit_code == 0 and not result.exception

def test_organisation_POST_datasets_list_valid_member_role(self):
"""As a SAAO member, I must be able to create a dataset."""
runner = CliRunner()
register_successful_organisation_login(runner, 'saao', 'member')
register_successful_login(runner, 'saao', 'member')
mock_http_post('/saao/datasets/', '[]')
result = runner.invoke(cli.datasets, ['create', '--organisation', 'saao', '-d'])
assert result.exit_code == 0 and not result.exception

def test_organisation_POST_datasets_list_invalid_guest_role(self):
"""As a SAAO guest, I must not be able to create a dataset."""
runner = CliRunner()
register_successful_organisation_login(runner, 'saao', 'guest')
register_successful_login(runner, 'saao', 'guest')
mock_http_post('/saao/datasets/', '[]')
result = runner.invoke(cli.datasets, ['create', '--organisation', 'saao', '-d'])
assert result.exit_code != 0 and isinstance(result.exception, ArcsecondError)
8 changes: 4 additions & 4 deletions tests/cli/test_datasets_personal.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@

from arcsecond import cli
from arcsecond.api.constants import ARCSECOND_API_URL_DEV
from tests.utils import register_successful_personal_login
from tests.utils import register_successful_login


@httpretty.activate
def test_empty_datasets_list():
runner = CliRunner()
register_successful_personal_login(runner)
register_successful_login(runner)

httpretty.register_uri(
httpretty.GET,
Expand All @@ -30,7 +30,7 @@ def test_empty_datasets_list():
@httpretty.activate
def test_datafiles_list_of_datasets():
runner = CliRunner()
register_successful_personal_login(runner)
register_successful_login(runner)

dataset_uuid = uuid.uuid4()
httpretty.register_uri(
Expand All @@ -48,7 +48,7 @@ def test_datafiles_list_of_datasets():
@httpretty.activate
def test_datafiles_create_with_file():
runner = CliRunner()
register_successful_personal_login(runner)
register_successful_login(runner)

dataset_uuid = uuid.uuid4()
httpretty.register_uri(
Expand Down
22 changes: 11 additions & 11 deletions tests/cli/test_nightlogs_organisations.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from click.testing import CliRunner
from unittest import TestCase

from arcsecond import cli, Arcsecond
from arcsecond import cli, ArcsecondAPI
from arcsecond.api.error import ArcsecondError
from arcsecond.config import config_file_clear_section, config_file_save_api_key, \
config_file_save_organisation_membership

from tests.utils import (register_successful_personal_login,
register_successful_organisation_login,
from tests.utils import (register_successful_login,
register_successful_login,
mock_http_get,
mock_http_post,
mock_url_path)
Expand All @@ -25,53 +25,53 @@ def tearDown(self):
def test_nightlogs_list_unlogged(self):
"""As a simple user, I must not be able to access the list of nightlogs of an organisation."""
runner = CliRunner()
register_successful_personal_login(runner)
register_successful_login(runner)
result = runner.invoke(cli.logs, ['--organisation', 'saao', '-d'])
assert result.exit_code != 0 and isinstance(result.exception, ArcsecondError)

def test_organisation_GET_nightlogs_list_logged_but_wrong_organisation(self):
"""No matter role I have, accessing an unknown organisation must fail."""
runner = CliRunner()
register_successful_organisation_login(runner, 'saao', 'superadmin')
register_successful_login(runner, 'saao', 'superadmin')
result = runner.invoke(cli.logs, ['--organisation', 'dummy', '-d'])
assert result.exit_code != 0 and isinstance(result.exception, ArcsecondError)

def test_organisation_GET_nightlogs_list_valid_role(self):
"""As a SAAO superadmin, I must be able to access the list of nightlogs."""
runner = CliRunner()
register_successful_organisation_login(runner, 'saao', 'superadmin')
register_successful_login(runner, 'saao', 'superadmin')
mock_http_get('/saao/nightlogs/', '[]')
result = runner.invoke(cli.logs, ['--organisation', 'saao', '-d'])
assert result.exit_code == 0 and not result.exception

def test_organisation_POST_nightlogs_list_valid_superadmin_role(self):
"""As a SAAO superadmin, I must be able to create a nightlog."""
runner = CliRunner()
register_successful_organisation_login(runner, 'saao', 'superadmin')
register_successful_login(runner, 'saao', 'superadmin')
mock_http_post('/saao/nightlogs/', '[]')
result = runner.invoke(cli.logs, ['create', '--organisation', 'saao', '-d'])
assert result.exit_code == 0 and not result.exception

def test_organisation_POST_nightlogs_list_valid_admin_role(self):
"""As a SAAO admin, I must be able to create a nightlog."""
runner = CliRunner()
register_successful_organisation_login(runner, 'saao', 'admin')
register_successful_login(runner, 'saao', 'admin')
mock_http_post('/saao/nightlogs/', '[]')
result = runner.invoke(cli.logs, ['create', '--organisation', 'saao', '-d'])
assert result.exit_code == 0 and not result.exception

def test_organisation_POST_nightlogs_list_valid_member_role(self):
"""As a SAAO member, I must be able to create a nightlog."""
runner = CliRunner()
register_successful_organisation_login(runner, 'saao', 'member')
register_successful_login(runner, 'saao', 'member')
mock_http_post('/saao/nightlogs/', '[]')
result = runner.invoke(cli.logs, ['create', '--organisation', 'saao', '-d'])
assert result.exit_code == 0 and not result.exception

def test_organisation_POST_nightlogs_list_invalid_guest_role(self):
"""As a SAAO guest, I must not be able to create a nightlog."""
runner = CliRunner()
register_successful_organisation_login(runner, 'saao', 'guest')
register_successful_login(runner, 'saao', 'guest')
mock_http_post('/saao/nightlogs/', '[]')
result = runner.invoke(cli.logs, ['create', '--organisation', 'saao', '-d'])
assert result.exit_code != 0 and isinstance(result.exception, ArcsecondError)
Expand All @@ -89,7 +89,7 @@ def tearDown(self):

def test_organisation_GET_nightlogs_filtered_list_valid_member_role(self):
"""As a simple user, I must not be able to access the list of nightlogs of an organisation."""
api = Arcsecond.build_nightlogs_api(debug=True, test=True, organisation='saao')
api = ArcsecondAPI.nightlogs(debug=True, test=True, organisation='saao')
mock_url_path(httpretty.GET, '/saao/nightlogs/', query='', body='[{uuid: "112233", name:"dummy"}]', status=200)
mock_url_path(httpretty.GET, '/saao/nightlogs/', query='?date=2020-03-28', body='[]', status=200)
logs, error = api.list(date='2020-03-28')
Expand Down
20 changes: 10 additions & 10 deletions tests/module/test_arcsecond_root.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
from arcsecond import Arcsecond
from arcsecond import ArcsecondAPI
from tests.utils import save_test_credentials, clear_test_credentials


def test_default_empty_state():
clear_test_credentials()
assert Arcsecond.is_logged_in(debug=True) is False
assert Arcsecond.username(debug=True) == ''
assert Arcsecond.memberships(debug=True) == {}
assert ArcsecondAPI.is_logged_in(debug=True) is False
assert ArcsecondAPI.username(debug=True) == ''
assert ArcsecondAPI.memberships(debug=True) == {}


def test_default_logged_in_state():
save_test_credentials('cedric')
assert Arcsecond.is_logged_in(debug=True) is True
assert Arcsecond.username(debug=True) == 'cedric'
assert Arcsecond.memberships(debug=True) == {}
assert ArcsecondAPI.is_logged_in(debug=True) is True
assert ArcsecondAPI.username(debug=True) == 'cedric'
assert ArcsecondAPI.memberships(debug=True) == {}


def test_default_logged_in_with_membership_state():
save_test_credentials('cedric', {'saao': 'superadmin'})
assert Arcsecond.is_logged_in(debug=True) is True
assert Arcsecond.username(debug=True) == 'cedric'
assert Arcsecond.memberships(debug=True) == {'saao': 'superadmin'}
assert ArcsecondAPI.is_logged_in(debug=True) is True
assert ArcsecondAPI.username(debug=True) == 'cedric'
assert ArcsecondAPI.memberships(debug=True) == {'saao': 'superadmin'}
12 changes: 6 additions & 6 deletions tests/module/test_datafiles_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
import httpretty
from click.testing import CliRunner

from arcsecond import Arcsecond
from arcsecond import ArcsecondAPI
from arcsecond.api.constants import ARCSECOND_API_URL_DEV
from tests.utils import register_successful_personal_login
from tests.utils import register_successful_login

has_callback_been_called = False

Expand All @@ -16,7 +16,7 @@
def test_datafiles_upload_file_threaded_no_callback():
# Using standard CLI runner to make sure we login successfuly as in other tests.
runner = CliRunner()
register_successful_personal_login(runner)
register_successful_login(runner)

dataset_uuid = uuid.uuid4()
httpretty.register_uri(
Expand All @@ -27,7 +27,7 @@ def test_datafiles_upload_file_threaded_no_callback():
)

# Go for Python module tests
datafiles_api = Arcsecond.build_datafiles_api(debug=True, dataset=str(dataset_uuid))
datafiles_api = ArcsecondAPI.datafiles(debug=True, dataset=str(dataset_uuid))
uploader, _ = datafiles_api.create({'file': os.path.abspath(__file__)})
uploader.start()
time.sleep(0.1)
Expand All @@ -41,7 +41,7 @@ def test_datafiles_upload_file_threaded_no_callback():
def test_datafiles_upload_file_threaded_with_callback():
# Using standard CLI runner to make sure we login successfuly as in other tests.
runner = CliRunner()
register_successful_personal_login(runner)
register_successful_login(runner)

dataset_uuid = uuid.uuid4()
httpretty.register_uri(
Expand All @@ -58,7 +58,7 @@ def upload_callback(eventName, progress):

fixtures_folder = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'fixtures')
# Go for Python module tests
datafiles_api = Arcsecond.build_datafiles_api(debug=True, dataset=str(dataset_uuid))
datafiles_api = ArcsecondAPI.datafiles(debug=True, dataset=str(dataset_uuid))
payload = {'file': os.path.join(fixtures_folder, 'file1.fits')}
uploader, _ = datafiles_api.create(payload, callback=upload_callback)
uploader.start()
Expand Down
26 changes: 4 additions & 22 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def make_profile_json(subdomain, role):
return json.dumps(make_profile(subdomain, role))


def register_successful_personal_login(runner):
def register_successful_login(runner, subdomain='robotland', role='member'):
httpretty.register_uri(
httpretty.POST,
ARCSECOND_API_URL_DEV + API_AUTH_PATH_LOGIN,
Expand All @@ -49,35 +49,17 @@ def register_successful_personal_login(runner):
)
httpretty.register_uri(
httpretty.GET,
ARCSECOND_API_URL_DEV + '/profiles/' + TEST_LOGIN_USERNAME + '/keys/',
status=200,
body='{ "api_key": "' + TEST_API_KEY + '" }'
)
result = runner.invoke(cli.login, ['-d'], input=TEST_LOGIN_USERNAME + '\n' + TEST_LOGIN_PASSWORD)
assert result.exit_code == 0


def register_successful_organisation_login(runner, subdomain, role):
httpretty.register_uri(
httpretty.POST,
ARCSECOND_API_URL_DEV + API_AUTH_PATH_LOGIN,
ARCSECOND_API_URL_DEV + '/profiles/' + TEST_LOGIN_USERNAME + '/',
status=200,
body='{ "key": "935e2b9e24c44581b4ef5f4c8e53213e935e2b9e24c44581b4ef5f4c8e53213e" }'
body=make_profile_json(subdomain, role)
)
httpretty.register_uri(
httpretty.GET,
ARCSECOND_API_URL_DEV + '/profiles/' + TEST_LOGIN_USERNAME + '/keys/',
status=200,
body='{ "api_key": "' + TEST_API_KEY + '" }'
)
httpretty.register_uri(
httpretty.GET,
ARCSECOND_API_URL_DEV + '/profiles/' + TEST_LOGIN_USERNAME + '/',
status=200,
body=make_profile_json(subdomain, role)
)
result = runner.invoke(cli.login, ['--organisation', subdomain, '-d'],
input=TEST_LOGIN_USERNAME + '\n' + TEST_LOGIN_PASSWORD)
result = runner.invoke(cli.login, ['-d'], input=TEST_LOGIN_USERNAME + '\n' + TEST_LOGIN_PASSWORD)
assert result.exit_code == 0


Expand Down

0 comments on commit 72f46a4

Please sign in to comment.