1+ import pytest
2+ import responses
3+ import time
4+ from firefly .ims_auth import AdobeIMSAuth
5+ from firefly .exceptions import FireflyAuthError
6+
7+ TOKEN_URL = "https://ims-na1.adobelogin.com/ims/token/v3"
8+
9+ def make_auth ():
10+ return AdobeIMSAuth (client_id = "dummy_id" , client_secret = "dummy_secret" , timeout = 5 )
11+
12+ @responses .activate
13+ def test_get_access_token_success ():
14+ responses .add (
15+ responses .POST ,
16+ TOKEN_URL ,
17+ json = {"access_token" : "test_token" , "expires_in" : 3600 },
18+ status = 200 ,
19+ )
20+ auth = make_auth ()
21+ token = auth .get_access_token ()
22+ assert token == "test_token"
23+ assert auth ._access_token == "test_token"
24+ assert auth ._token_expiry > time .time ()
25+
26+ @responses .activate
27+ def test_token_caching ():
28+ responses .add (
29+ responses .POST ,
30+ TOKEN_URL ,
31+ json = {"access_token" : "cached_token" , "expires_in" : 3600 },
32+ status = 200 ,
33+ )
34+ auth = make_auth ()
35+ token1 = auth .get_access_token ()
36+ # Should use cached token on second call
37+ token2 = auth .get_access_token ()
38+ assert token1 == token2
39+ assert responses .assert_call_count (TOKEN_URL , 1 ) is True
40+
41+ @responses .activate
42+ def test_token_expiry ():
43+ responses .add (
44+ responses .POST ,
45+ TOKEN_URL ,
46+ json = {"access_token" : "first_token" , "expires_in" : 1 },
47+ status = 200 ,
48+ )
49+ auth = make_auth ()
50+ token1 = auth .get_access_token ()
51+ # Simulate token expiry
52+ auth ._token_expiry = time .time () - 10
53+ responses .add (
54+ responses .POST ,
55+ TOKEN_URL ,
56+ json = {"access_token" : "second_token" , "expires_in" : 3600 },
57+ status = 200 ,
58+ )
59+ token2 = auth .get_access_token ()
60+ assert token2 == "second_token"
61+ assert token1 != token2
62+
63+ @responses .activate
64+ def test_get_access_token_failure ():
65+ responses .add (
66+ responses .POST ,
67+ TOKEN_URL ,
68+ json = {"error" : "invalid_client" },
69+ status = 401 ,
70+ )
71+ auth = make_auth ()
72+ with pytest .raises (FireflyAuthError ):
73+ auth .get_access_token ()
74+
75+ @responses .activate
76+ def test_get_access_token_network_error ():
77+ responses .add (
78+ responses .POST ,
79+ TOKEN_URL ,
80+ body = Exception ("network error" ),
81+ )
82+ auth = make_auth ()
83+ with pytest .raises (FireflyAuthError ):
84+ auth .get_access_token ()
85+
86+ @responses .activate
87+ def test_get_access_token_bad_response ():
88+ responses .add (
89+ responses .POST ,
90+ TOKEN_URL ,
91+ json = {"not_access_token" : True },
92+ status = 200 ,
93+ )
94+ auth = make_auth ()
95+ with pytest .raises (FireflyAuthError ):
96+ auth .get_access_token ()
0 commit comments