def test_auth_send_request(app, http_client, auth_server, auth_path): body = b'{"user_id": 0}' auth_server.add_response(services.Request('POST', auth_path), services.Response(200, body=body)) response = yield auth.AccountingServerAuth.send_request('foobar') assert response.code == 200 assert response.body == body
def test_content_type_header_set(app, http_client, auth_server, auth_path): body = b'{"user_id": 0}' auth_server.add_response(services.Request('POST', auth_path), services.Response(200, body=body)) response = yield auth.AccountingServerAuth.send_request('foobar') assert response.code == 200 request = auth_server.get_request(auth_path) assert request.headers['Content-Type'] == 'application/json'
def test_auth_returns_inactive_user_object(app, http_client, auth_server, auth_path): body = b'{"user_id": 0, "active": false, "block_quota": 512, "monthly_traffic_quota": 123456789}' auth_server.add_response(services.Request('POST', auth_path), services.Response(200, body=body)) user = yield auth.AccountingServerAuth.request_auth('foobar') assert user.user_id == 0 assert not user.is_active
def test_prefix_auth(backend, http_client, prefix_path, user_id, pg_db, auth_server, auth_path): auth_server.add_response(services.Request('POST', auth_path), services.Response(404)) response = yield http_client.fetch(prefix_path, method='POST', raise_error=False, allow_nonstandard_methods=True) assert response.code == 403 response = yield http_client.fetch(prefix_path, method='POST', raise_error=False, headers={'Authorization': 'Token Foobar'}, allow_nonstandard_methods=True) assert response.code == 403
def test_auth_backend_called(backend, cache, http_client, path, auth_path, headers, auth_server, file_path): body = b'Dummy' _, prefix, file_name = file_path.split('/') auth_server.add_response(services.Request('POST', auth_path), services.Response(200, body=b'{"user_id": 0, "active": true,' b'"block_quota": 123, "monthly_traffic_quota": 789}')) response = yield http_client.fetch(path, method='POST', body=body, headers=headers, raise_error=False) auth_request = auth_server.get_request(auth_path) assert auth_request.headers['APISECRET'] == options.apisecret assert response.code == 204, response.body
def test_log_and_monitoring(app, mocker, http_client, path, auth_path, headers, auth_server, file_path, prefix): mon_traffic = stat_by_name('block_traffic_by_request_sum') mon_quota = stat_by_name('block_quota_by_request_sum') traffic_before = mon_traffic() or 0 quota_before = mon_quota({'type': 'increase'}) or 0 quota_dec_before = mon_quota({'type': 'decrease'}) or 0 quota_log = mocker.patch( 'blockserver.backend.database.PostgresUserDatabase.update_size') trafifc_log = mocker.patch( 'blockserver.backend.database.PostgresUserDatabase.update_traffic') body = b'Dummy' size = len(body) _, prefix_name, file_name = file_path.split('/') auth_server.add_response( services.Request('POST', auth_path), services.Response( 200, body=b'{"user_id": 0, "active": true,' b'"block_quota": 123, "monthly_traffic_quota": 789}')) yield http_client.fetch(path, method='POST', body=body, headers=headers) quota = mon_quota({'type': 'increase'}) assert quota - quota_before == size yield http_client.fetch(path, method='POST', body=body, headers=headers) quota = mon_quota({'type': 'increase'}) assert quota - quota_before == size yield http_client.fetch(path, method='GET', headers=headers) yield http_client.fetch(path, method='DELETE', headers=headers) assert mon_traffic() - traffic_before == size assert (mon_quota({'type': 'decrease'}) - quota_dec_before) == size expected_quota = [call(prefix, size), call(prefix, -size)] assert quota_log.call_args_list == expected_quota expected_traffic = [call(prefix, size)] assert trafifc_log.call_args_list == expected_traffic
def test_auth_send_request_error_propagation(app, http_client, auth_server, auth_path): auth_server.add_response(services.Request('POST', auth_path), services.Response(500)) with pytest.raises(auth.AuthError): yield auth.AccountingServerAuth.send_request('foobar')
def test_auth_send_request_not_found(app, http_client, auth_server, auth_path): auth_server.add_response(services.Request('POST', auth_path), services.Response(404)) with pytest.raises(auth.UserNotFound): yield auth.AccountingServerAuth.send_request('foobar')