one.tests.test_alyxclient

Unit tests for the one.webclient module.

Functions

setUpModule

Point the parameter files at a directory belonging to this module.

tearDownModule

Classes

TestAuthentication

Tests for AlyxClient authentication, token storage, login/out methods and user prompts.

TestDownloadHTTP

TestJsonFieldMethods

Tests for AlyxClient methods that modify the JSON field of a REST endpoint.

TestMisc

TestRestCache

Tests for REST caching system, the cache decorator and cache flags.

TestRestDocumentation

Tests for AlyxClient REST API schema parsing and printing.

TestThrottleBackoff

Test behaviour when AlyxClient receives a 429 Too Many Requests response.

setUpModule()[source]

Point the parameter files at a directory belonging to this module.

These tests log in and out, which rewrites the parameter file for whichever database they use - the developer’s real ~/.one without this. The test database is then set up silently so no class depends on another having run first.

tearDownModule()[source]
class TestRestDocumentation(methodName='runTest')[source]

Bases: TestCase

Tests for AlyxClient REST API schema parsing and printing.

setUp() → None[source]

Hook method for setting up the test fixture before exercising it.

test_rest_schema_core_api()[source]

Test parsing of coreapi REST schema.

test_print_endpoint_info()[source]

Test endpoint query params are printed when calling AlyxClient.rest without action.

test_alyx_client_methods()[source]

Test AlyxClient.list_endpoints and AlyxClient.print_endpoint_info.

test_schema_support()[source]

Test that both coreapi and openapiv3 REST schemas are supported.

class TestAuthentication(methodName='runTest')[source]

Bases: TestCase

Tests for AlyxClient authentication, token storage, login/out methods and user prompts.

setUp() → None[source]

Hook method for setting up the test fixture before exercising it.

test_authentication()[source]

Test for AlyxClient.authenticate and AlyxClient.is_logged_in property.

test_authenticate_with_token()[source]

Test authenticating with a token in place of a password.

Accounts that sign in through an identity provider have no password at all, so /auth-token is closed to them and a token is the only credential they have.

test_authenticate_with_token_not_cached()[source]

A token passed with cache_token=False should not be written to the params.

test_authenticate_token_prompt()[source]

A blank password at the prompt should fall through to asking for a token.

test_verify_token_rejects_bad_token()[source]

A token the database refuses must be discarded rather than cached.

test_verify_token_tolerates_unreachable_database()[source]

A network failure while checking must not reject a token that may be perfectly good.

test_verify_token_checks_the_me_endpoint()[source]

The token is checked against /me, which also reports whose it is.

test_token_username_wins_over_the_one_supplied()[source]

The database knows whose token it is; caching it under another name would lie.

test_token_alone_identifies_the_user()[source]

A token is the one credential that can name its owner, so no username is needed.

test_a_token_is_never_asked_who_it_belongs_to()[source]

A token names its own owner, so neither the prompt nor the stored login applies.

test_the_api_version_survives_every_authentication_path()[source]

A database can only see how old its clients are if they all say so.

authenticate rebuilds the header dict, so the version was previously sent only by a client that authenticated through the constructor, and dropped by one that authenticated lazily or from a cached token - the common case by far.

test_the_api_version_is_sent_with_the_request()[source]

The header must reach the wire, not merely sit in the client’s dict.

test_no_username_is_an_error_rather_than_a_prompt_loop()[source]

With a blank default login and no token, there is nothing to authenticate as.

test_a_token_alone_needs_a_database_that_names_its_owner()[source]

An Alyx too old to serve /me as an API cannot say whose token it is.

With a token alone there is no name to cache it under and no way to tell whether it is even the intended account, so it is refused.

test_a_token_alone_is_refused_when_the_database_is_unreachable()[source]

The other way the check comes back empty: it never reached the database.

test_a_username_resolved_once_is_remembered()[source]

Otherwise the cached token, which is keyed by username, could never be found again.

test_auth_methods()[source]

Test behaviour when calling AlyxClient._generic_request when logged out.

test_download_cache_tables_authenticates()[source]

Downloading the cache tables must authenticate first, like any other request.

test_auth_errors()[source]

Test behaviour when authentication fails.

class TestJsonFieldMethods(methodName='runTest')[source]

Bases: TestCase

Tests for AlyxClient methods that modify the JSON field of a REST endpoint.

These tests are over-engineered in order to test Alyx Django queries with JSON fields. Django queries are also tested in TestRemote.test_search.

setUp()[source]

Hook method for setting up the test fixture before exercising it.

test_json_methods()[source]

Test for AlyxClient.json_field* methods (write, update, remove_key and delete).

test_empty()[source]

Test for AlyxClient.json_field* methods when JSON field is empty.

test_uuid_serialize()[source]

Check that UUID objects are serialized to JSON.

class TestRestCache(methodName='runTest')[source]

Bases: TestCase

Tests for REST caching system, the cache decorator and cache flags.

setUp()[source]

Hook method for setting up the test fixture before exercising it.

test_loads_cached()[source]

Test for one.webclient._cache_response decorator, checks returns cached result.

test_expired_cache()[source]

Test behaviour when cached REST query is expired.

test_caches_response()[source]

Test caches query response before returning.

test_cache_mode()[source]

Test for AlyxClient.cache_mode property.

test_expiry_param()[source]

Test for expires kwarg in one.webclient._cache_response decorator.

test_cache_returned_on_error()[source]

Test behaviour when connection error occurs and cached response exists.

test_decode_error_cache()[source]

Test behaviour when cached file is corrupted.

test_clear_cache()[source]

Test for AlyxClient.clear_rest_cache.

class TestThrottleBackoff(methodName='runTest')[source]

Bases: TestCase

Test behaviour when AlyxClient receives a 429 Too Many Requests response.

setUp()[source]

Hook method for setting up the test fixture before exercising it.

test_backoff_with_retry_after(sleep_mock)[source]

Test that AlyxClient respects Retry-After header.

test_backoff_without_retry_after(sleep_mock)[source]

Test that AlyxClient uses exponential backoff when Retry-After header is missing.

test_maximum_thresholds(sleep_mock)[source]

Test that AlyxClient raises an error after maximum retry attempts.

test_retry_after_date(sleep_mock)[source]

Test that AlyxClient correctly parses Retry-After header when it is a date.

class TestDownloadHTTP(methodName='runTest')[source]

Bases: TestCase

setUp()[source]

Hook method for setting up the test fixture before exercising it.

test_download_datasets_with_api()[source]
test_download_creates_a_missing_target_directory()[source]

The default target is ~/Downloads, which plenty of machines do not have.

test_download_datasets()[source]
test_download_cache_tables_auth(download_file_mock, zipfile_mock)[source]

Test for AlyxClient.download_cache_tables with authentication.

NB: This test simply checks that alyx is authenticated automatically before downloading the tables.

test_http_server_auth(open_mock, urllib_mock)[source]

Test for http_download_file authentication and headers.

class TestMisc(methodName='runTest')[source]

Bases: TestCase

setUp()[source]

Hook method for setting up the test fixture before exercising it.

test_update_url_params()[source]

Test for one.webclient.update_url_params.

test_validate_file_url()[source]

Test for AlyxClient._validate_file_url.

test_no_cache_context_manager()[source]

Test for one.webclient.no_cache function.

test_cache_dir_setter()[source]

Tests setter for AlyxClient.cache_dir attribute.

test_alyx_advice_header()[source]

Test that the Alyx-API-Notice response header is logged or raised as a warning.

test_lifecycle_headers()[source]

Test that the Deprecation and Sunset response headers are logged.

test_paginated_response()[source]

Test the _PaginatedResponse class.

test_paginated_response_count_changed()[source]

Test _PaginatedResponse when records are added/removed by another process.

The remote count is fixed at instantiation, so a -ve index is resolved against a count that may be stale by the time the page is fetched. Previously this left None values in the cache when records were removed.

test_json_encoder()[source]

Test that the JSONEncoder subclass serializes UUID objects.