|
| 1 | +"""Split API tests module.""" |
| 2 | + |
| 3 | +import pytest |
| 4 | +from splitio.api import auth, client, APIException |
| 5 | +from splitio.client.util import get_metadata |
| 6 | +from splitio.client.config import DEFAULT_CONFIG |
| 7 | +from splitio.version import __version__ |
| 8 | + |
| 9 | + |
| 10 | +class AuthAPITests(object): |
| 11 | + """Auth API test cases.""" |
| 12 | + |
| 13 | + def test_auth(self, mocker): |
| 14 | + """Test auth API call.""" |
| 15 | + token = "eyJhbGciOiJIUzI1NiIsImtpZCI6IjVZOU05US45QnJtR0EiLCJ0eXAiOiJKV1QifQ.eyJ4LWFibHktY2FwYWJpbGl0eSI6IntcIk56TTJNREk1TXpjMF9NVGd5TlRnMU1UZ3dOZz09X3NlZ21lbnRzXCI6W1wic3Vic2NyaWJlXCJdLFwiTnpNMk1ESTVNemMwX01UZ3lOVGcxTVRnd05nPT1fc3BsaXRzXCI6W1wic3Vic2NyaWJlXCJdLFwiY29udHJvbF9wcmlcIjpbXCJzdWJzY3JpYmVcIixcImNoYW5uZWwtbWV0YWRhdGE6cHVibGlzaGVyc1wiXSxcImNvbnRyb2xfc2VjXCI6W1wic3Vic2NyaWJlXCIsXCJjaGFubmVsLW1ldGFkYXRhOnB1Ymxpc2hlcnNcIl19IiwieC1hYmx5LWNsaWVudElkIjoiY2xpZW50SWQiLCJleHAiOjE2MDIwODgxMjcsImlhdCI6MTYwMjA4NDUyN30.5_MjWonhs6yoFhw44hNJm3H7_YMjXpSW105DwjjppqE" |
| 16 | + httpclient = mocker.Mock(spec=client.HttpClient) |
| 17 | + payload = '{{"pushEnabled": true, "token": "{token}"}}'.format(token=token) |
| 18 | + cfg = DEFAULT_CONFIG.copy() |
| 19 | + cfg.update({'IPAddressesEnabled': True, 'machineName': 'some_machine_name', 'machineIp': '123.123.123.123'}) |
| 20 | + sdk_metadata = get_metadata(cfg) |
| 21 | + httpclient.get.return_value = client.HttpResponse(200, payload) |
| 22 | + auth_api = auth.AuthAPI(httpclient, 'some_api_key', sdk_metadata) |
| 23 | + response = auth_api.authenticate() |
| 24 | + |
| 25 | + assert response.push_enabled == True |
| 26 | + assert response.token == token |
| 27 | + |
| 28 | + call_made = httpclient.get.mock_calls[0] |
| 29 | + |
| 30 | + # validate positional arguments |
| 31 | + assert call_made[1] == ('auth', '/auth', 'some_api_key') |
| 32 | + |
| 33 | + # validate key-value args (headers) |
| 34 | + assert call_made[2]['extra_headers'] == { |
| 35 | + 'SplitSDKVersion': 'python-%s' % __version__, |
| 36 | + 'SplitSDKMachineIP': '123.123.123.123', |
| 37 | + 'SplitSDKMachineName': 'some_machine_name' |
| 38 | + } |
| 39 | + # assert httpclient.get.mock_calls == [mocker.call('auth', '/auth', 'some_api_key', )] |
| 40 | + |
| 41 | + httpclient.reset_mock() |
| 42 | + def raise_exception(*args, **kwargs): |
| 43 | + raise client.HttpClientException('some_message') |
| 44 | + httpclient.get.side_effect = raise_exception |
| 45 | + with pytest.raises(APIException) as exc_info: |
| 46 | + response = auth_api.authenticate() |
| 47 | + assert exc_info.type == APIException |
| 48 | + assert exc_info.value.message == 'some_message' |
0 commit comments