def test_refresh_after_expiration(self): def token_callback(request): if 'grant_type=authorization_code' in request.body: filename = 'access_token.json' elif 'grant_type=refresh_token' in request.body: filename = 'access_token_refreshed.json' else: raise ValueError('What is your grant type?') with open('tests/fixtures/json/' + filename) as f: return 200, {}, f.read() responses.add_callback(responses.POST, 'https://api.mediamath.com/oauth2/v1.0/token', callback=token_callback, content_type='application/json') token = self.t1.fetch_token(state='state', code='code') now = time() token.update({ 'expiration_time': int(now) - 1, 'expires_at': now - 1, }) with open('tests/fixtures/xml/advertiser.xml') as f: responses.add(responses.GET, 'https://api.mediamath.com/api/v2.0/advertisers/1', body=f.read(), content_type='application/xml') new_t1 = T1(token=token, token_updater=mock_saver, **mock_credentials) _ = new_t1.get('advertisers', 1) self.assertEqual(new_t1.session.token['access_token'], 'newaccesstoken') self.assertEqual(new_t1.session.token['refresh_token'], 'newrefreshtoken')
def setup(user_credentials, use_json): t1 = T1(auth_method='cookie', api_base=API_BASE, json=use_json, **user_credentials) assert hasattr(t1, 'user_id'), 'No user ID present' return t1
def test_incorrect_login_raises_error(self): def login_callback(_): with open('tests/fixtures/json/auth_error.json') as f: body = f.read() return 401, {}, body mock_credentials = { 'username': '******', 'password': '******', 'api_key': 'api_key', } responses.add_callback( responses.POST, 'https://api.mediamath.com/api/v2.0/login', content_type='application/vnd.mediamath.v1+json', callback=login_callback) with self.assertRaises(errors.AuthRequiredError) as cm: T1(auth_method='cookie', api_base=API_BASE, json=True, **mock_credentials) exc = cm.exception self.assertEqual(exc.message, 'Authentication error')
def test_correct_login_works(self): expected_session = 'd24f41277ae4c202dda876676b0006585b20f64f' expected_user = '******' expected_user_id = 1 def login_callback(_): with open('tests/fixtures/json/session.json') as f: body = f.read() response_headers = { 'Set-Cookie': 'adama_session=' + expected_session, } return 200, response_headers, body mock_credentials = { 'username': expected_user, 'password': '******', 'api_key': 'api_key', } responses.add_callback( responses.POST, 'https://api.mediamath.com/api/v2.0/login', callback=login_callback, content_type='application/vnd.mediamath.v1+json') t1 = T1(auth_method='cookie', json=True, api_base=API_BASE, **mock_credentials) assert hasattr(t1, 'user_id'), 'No user ID present' assert (t1.user_id == expected_user_id), 'incorrect user ID returned' assert (t1.username == expected_user), 'user name is incorrect' assert (t1.session_id == expected_session), 'session id not correct'
def setup_oauth(user_credentials, use_json): t1 = T1(auth_method='oauth2-resourceowner', api_base=API_BASE, json=use_json, environment='sandbox', **user_credentials) assert hasattr(t1, 'user_id'), 'No user ID present' return t1
def setUp(self): """Setup.""" with open('tests/fixtures/json/access_token.json') as f: token = f.read() responses.add(responses.POST, 'https://auth.mediamath.com/oauth/token', body=token, adding_headers={ 'Set-Cookie': 'adama_session=1', }, content_type='application/json') self.t1 = T1(auth_method='oauth2-resourceowner', **mock_credentials)
def setUp(self): """set up test fixtures""" with open('tests/fixtures/xml/session.xml') as f: fixture = f.read() responses.add(responses.POST, 'https://api.mediamath.com/api/v2.0/login', body=fixture, adding_headers={ 'Set-Cookie': 'adama_session=1', }, content_type='application/xml') self.t1 = T1(auth_method='cookie', api_base=API_BASE, **mock_credentials)
def test_new_session_with_token(self): responses.add(responses.POST, 'https://api.mediamath.com/oauth2/v1.0/token', body=self.token, content_type='application/json') token = self.t1.fetch_token(state='state', code='code') with open('tests/fixtures/xml/advertiser.xml') as f: responses.add(responses.GET, 'https://api.mediamath.com/api/v2.0/advertisers/1', body=f.read(), content_type='application/xml') new_t1 = T1(token=token, token_updater=mock_saver, **mock_credentials) adv = new_t1.get('advertisers', 1) self.assertEqual(adv.id, 1)
def test_no_user_fails(self): def login_callback(_): with open('tests/fixtures/json/login_badrequest.json') as f: body = f.read() return 400, {}, body mock_credentials = { 'password': '******', 'api_key': 'api_key', } responses.add_callback( responses.POST, 'https://api.mediamath.com/api/v2.0/login', content_type='application/vnd.mediamath.v1+json', callback=login_callback) with self.assertRaises(errors.ValidationError) as cm: T1(auth_method='cookie', api_base=API_BASE, **mock_credentials) exc = cm.exception self.assertEqual('invalid', exc.message)
def test_no_key_fails(self): def login_callback(_): with open('tests/fixtures/xml/login_no_key.xml') as f: body = f.read() return 403, {}, body mock_credentials = { 'username': '******', 'password': '******', } responses.add_callback(responses.POST, 'https://api.mediamath.com/api/v2.0/login', content_type='text/xml', callback=login_callback) with self.assertRaises(errors.T1Error) as cm: T1(auth_method='cookie', json=True, api_base=API_BASE, **mock_credentials) exc = cm.exception self.assertEqual(b'<h1>Developer Inactive</h1>', exc.message)
def test_session_id(t1): t2 = T1(session_id=t1.session_id, api_base=API_BASE, auth_method='cookie') assert hasattr(t2, 'username'), 'Expected new T1 session, got: %r' % t2
def setup(credentials): return T1(auth_method='cookie', **credentials)
def setUp(self): self.t1 = T1(**mock_credentials) with open('tests/fixtures/json/access_token.json') as f: self.token = f.read()