|
| 1 | +import responses |
| 2 | +from watson_developer_cloud import iam_token_manager |
| 3 | +import time |
| 4 | + |
| 5 | +@responses.activate |
| 6 | +def test_request_token(): |
| 7 | + iam_url = "https://iam.bluemix.net/identity/token" |
| 8 | + response = """{ |
| 9 | + "access_token": "oAeisG8yqPY7sFR_x66Z15", |
| 10 | + "token_type": "Bearer", |
| 11 | + "expires_in": 3600, |
| 12 | + "expiration": 1524167011, |
| 13 | + "refresh_token": "jy4gl91BQ" |
| 14 | + }""" |
| 15 | + responses.add(responses.POST, url=iam_url, body=response, status=200) |
| 16 | + |
| 17 | + token_manager = iam_token_manager.IAMTokenManager("iam_api_key", "iam_access_token", iam_url) |
| 18 | + token_manager._request_token() |
| 19 | + |
| 20 | + assert responses.calls[0].request.url == iam_url |
| 21 | + assert responses.calls[0].response.text == response |
| 22 | + assert len(responses.calls) == 1 |
| 23 | + |
| 24 | +@responses.activate |
| 25 | +def test_refresh_token(): |
| 26 | + iam_url = "https://iam.bluemix.net/identity/token" |
| 27 | + response = """{ |
| 28 | + "access_token": "oAeisG8yqPY7sFR_x66Z15", |
| 29 | + "token_type": "Bearer", |
| 30 | + "expires_in": 3600, |
| 31 | + "expiration": 1524167011, |
| 32 | + "refresh_token": "jy4gl91BQ" |
| 33 | + }""" |
| 34 | + responses.add(responses.POST, url=iam_url, body=response, status=200) |
| 35 | + |
| 36 | + token_manager = iam_token_manager.IAMTokenManager("iam_api_key", "iam_access_token", iam_url) |
| 37 | + token_manager._refresh_token() |
| 38 | + |
| 39 | + assert responses.calls[0].request.url == iam_url |
| 40 | + assert responses.calls[0].response.text == response |
| 41 | + assert len(responses.calls) == 1 |
| 42 | + |
| 43 | +@responses.activate |
| 44 | +def test_is_token_expired(): |
| 45 | + token_manager = iam_token_manager.IAMTokenManager("iam_api_key", "iam_access_token", "iam_url") |
| 46 | + token_manager.token_info = { |
| 47 | + "access_token": "oAeisG8yqPY7sFR_x66Z15", |
| 48 | + "token_type": "Bearer", |
| 49 | + "expires_in": 3600, |
| 50 | + "expiration": int(time.time()) + 6000, |
| 51 | + "refresh_token": "jy4gl91BQ" |
| 52 | + } |
| 53 | + assert token_manager._is_token_expired() is False |
| 54 | + token_manager.token_info['expiration'] = int(time.time()) - 3600 |
| 55 | + assert token_manager._is_token_expired() |
| 56 | + |
| 57 | +@responses.activate |
| 58 | +def test_is_refresh_token_expired(): |
| 59 | + token_manager = iam_token_manager.IAMTokenManager("iam_api_key", "iam_access_token", "iam_url") |
| 60 | + token_manager.token_info = { |
| 61 | + "access_token": "oAeisG8yqPY7sFR_x66Z15", |
| 62 | + "token_type": "Bearer", |
| 63 | + "expires_in": 3600, |
| 64 | + "expiration": int(time.time()), |
| 65 | + "refresh_token": "jy4gl91BQ" |
| 66 | + } |
| 67 | + assert token_manager._is_refresh_token_expired() is False |
| 68 | + token_manager.token_info['expiration'] = int(time.time()) - (8 * 24 * 3600) |
| 69 | + assert token_manager._is_token_expired() |
| 70 | + |
| 71 | +@responses.activate |
| 72 | +def test_get_token(): |
| 73 | + iam_url = "https://iam.bluemix.net/identity/token" |
| 74 | + token_manager = iam_token_manager.IAMTokenManager("iam_api_key", iam_url=iam_url) |
| 75 | + token_manager.user_access_token = 'user_access_token' |
| 76 | + |
| 77 | + # Case 1: |
| 78 | + token = token_manager.get_token() |
| 79 | + assert token == token_manager.user_access_token |
| 80 | + |
| 81 | + # Case 2: |
| 82 | + token_manager.user_access_token = '' |
| 83 | + response = """{ |
| 84 | + "access_token": "hellohello", |
| 85 | + "token_type": "Bearer", |
| 86 | + "expires_in": 3600, |
| 87 | + "expiration": 1524167011, |
| 88 | + "refresh_token": "jy4gl91BQ" |
| 89 | + }""" |
| 90 | + responses.add(responses.POST, url=iam_url, body=response, status=200) |
| 91 | + token = token_manager.get_token() |
| 92 | + assert token == "hellohello" |
| 93 | + |
| 94 | + # Case 3: |
| 95 | + token_manager.token_info['expiration'] = int(time.time()) - (20 * 24 * 3600) |
| 96 | + token = token_manager.get_token() |
| 97 | + assert "grant_type=urn" in responses.calls[1].request.body |
| 98 | + token_manager.token_info['expiration'] = int(time.time()) - 4000 |
| 99 | + token = token_manager.get_token() |
| 100 | + assert "grant_type=refresh_token" in responses.calls[2].request.body |
| 101 | + |
| 102 | + # Case 4 |
| 103 | + token_manager.token_info = { |
| 104 | + "access_token": "dummy", |
| 105 | + "token_type": "Bearer", |
| 106 | + "expires_in": 3600, |
| 107 | + "expiration": int(time.time()) + 3600, |
| 108 | + "refresh_token": "jy4gl91BQ" |
| 109 | + } |
| 110 | + token = token_manager.get_token() |
| 111 | + assert token == 'dummy' |
0 commit comments