def testGetAccessTokenURLforRefreshToken(self): sample_access_token = accesstoken.AccessToken( self._authorization_server, 'refresh_token', self._options) self.assertEqual( sample_access_token.get_access_token_url(), ('https://authn.sd00.worldcat.org/oauth2/accessToken?' + 'grant_type=refresh_token' + '&refresh_token=rt_25fXauhJC09E4kwFxcf4TREkTnaRYWHgJA0W'))
def testGetAccessTokenURLforClientCredentials(self): sample_access_token = accesstoken.AccessToken( self._authorization_server, 'client_credentials', self._options) self.assertEqual( sample_access_token.get_access_token_url(), ('https://authn.sd00.worldcat.org/oauth2/accessToken?' + 'grant_type=client_credentials&' + 'authenticatingInstitutionId=128807&' + 'contextInstitutionId=128808&' + 'scope=WMS_NCIP WMS_ACQ'))
def testGetAccessTokenURLforAuthorizationCode(self): sample_access_token = accesstoken.AccessToken( self._authorization_server, 'authorization_code', self._options) self.assertEqual( sample_access_token.get_access_token_url(), ('https://authn.sd00.worldcat.org/oauth2/accessToken?' + 'grant_type=authorization_code' + '&code=unknown' + '&authenticatingInstitutionId=128807' + '&contextInstitutionId=128808' + '&redirect_uri=ncip%3A%2F%2Ftestapp'))
def testStringRepresenationOfClass(self): """Create a new access token which hasn't been authenticated yet.""" sample_access_token = accesstoken.AccessToken( self._authorization_server, grant_type='authorization_code', options={'scope': ['WMS_NCIP', 'WMS_ACQ'], 'authenticating_institution_id': '128807', 'context_institution_id': '128808', 'redirect_uri': 'https://localhost:8000/auth/', 'code': 'unknown' } ) """Assume authentication has occured and these parameters are now filled in.""" sample_access_token.expires_at = '2014-04-08 13:38:29Z' sample_access_token.expires_in = 1198 sample_access_token.access_token_string = 'tk_TBHrsDbSrWW1oS7d3gZr7NJb7PokyOFlf0pr' sample_access_token.type = 'bearer' sample_access_token.error_code = 404 sample_access_token.error_message = 'No reply at all.' sample_access_token.error_url = 'http://www.noreply.oclc.org/auth/' self.assertEqual(str(sample_access_token), ( "\n" + "access_token_url: https://authn.sd00.worldcat.org/oauth2/accessToken?\n" + " grant_type=authorization_code\n" + " &code=unknown\n" + " &authenticatingInstitutionId=128807\n" + " &contextInstitutionId=128808\n" + " &redirect_uri=https%3A%2F%2Flocalhost%3A8000%2Fauth%2F\n" + "\n" + "access_token_string tk_TBHrsDbSrWW1oS7d3gZr7NJb7PokyOFlf0pr\n" + "authenticating_institution_id: 128807\n" + "authorization_server: https://authn.sd00.worldcat.org/oauth2\n" + "code: unknown\n" + "context_institution_id: 128808\n" + "error_code: 404\n" + "error_message: No reply at all.\n" + "error_url: http://www.noreply.oclc.org/auth/\n" + "expires_at: 2014-04-08 13:38:29Z\n" + "expires_in: 1198\n" + "grant_type: authorization_code\n" + "options: None\n" + "redirect_uri: https://localhost:8000/auth/\n" + "refresh_token:\n" + "None\n" + "scope: ['WMS_NCIP', 'WMS_ACQ']\n" + "type: bearer\n" + "user:\n" + "None\n" + "wskey:\n" + "None") )
def testParseTokenResponse(self): sample_access_token = accesstoken.AccessToken( self._authorization_server, 'authorization_code', self._options) sample_access_token.parse_token_response( '{' + '"expires_at":"2014-03-13 15:44:59Z",' + '"principalIDNS":"urn:oclc:platform:128807",' + '"principalID":"2334dd24-b27e-49bd-8fea-7cc8de670f8d",' + '"error_code":"trouble",' + '"expires_in":1199,' + '"token_type":"bearer",' + '"context_institution_id":"128807",' + '"access_token":"tk_25fXauhJC09E5kwFxcf4TRXkTnaRYWHgJA0W",' + '"refresh_token":"rt_25fXauhJC09E5kwFxcf4TRXkTnaRYWHgJA0W",' + '"refresh_token_expires_in":1900,' + '"refresh_token_expires_at":"2014-03-13 15:44:59Z"' + '}') expected_user = user.User( authenticating_institution_id='128807', principal_id='2334dd24-b27e-49bd-8fea-7cc8de670f8d', principal_idns='urn:oclc:platform:128807') expected_refresh_token = refreshtoken.RefreshToken( tokenValue='rt_25fXauhJC09E5kwFxcf4TRXkTnaRYWHgJA0W', expires_in=1900, expires_at='2014-03-13 15:44:59Z') self.assertEqual(sample_access_token.access_token_string, 'tk_25fXauhJC09E5kwFxcf4TRXkTnaRYWHgJA0W') self.assertEqual(sample_access_token.type, 'bearer') self.assertEqual(sample_access_token.expires_at, '2014-03-13 15:44:59Z') self.assertEqual(sample_access_token.expires_in, 1199) self.assertEqual(sample_access_token.error_code, 'trouble') self.assertEqual(sample_access_token.context_institution_id, '128807') self.assertEqual(user.User, type(sample_access_token.user)) self.assertEqual( expected_user.authenticating_institution_id, sample_access_token.user.authenticating_institution_id) self.assertEqual(expected_user.principal_id, sample_access_token.user.principal_id) self.assertEqual(expected_user.principal_idns, sample_access_token.user.principal_idns) self.assertEqual(refreshtoken.RefreshToken, type(sample_access_token.refresh_token)) self.assertEqual(expected_refresh_token.refresh_token, sample_access_token.refresh_token.refresh_token) self.assertEqual(expected_refresh_token.expires_in, sample_access_token.refresh_token.expires_in) self.assertEqual(expected_refresh_token.expires_at, sample_access_token.refresh_token.expires_at)
def setUp(self): self._my_refresh_token = refreshtoken.RefreshToken( tokenValue='rt_25fXauhJC09E4kwFxcf4TREkTnaRYWHgJA0W', expires_in=1199, expires_at='2014-03-13 15:44:59Z') self._options = { 'scope': ['WMS_NCIP', 'WMS_ACQ'], 'authenticating_institution_id': '128807', 'context_institution_id': '128808', 'redirect_uri': 'ncip://testapp', 'refresh_token': self._my_refresh_token, 'code': 'unknown' } self._authorization_server = 'https://authn.sd00.worldcat.org/oauth2' self._my_access_token = accesstoken.AccessToken( self._authorization_server, 'authorization_code', self._options)
def setUp(self): """Create a new access token which hasn't been authenticated yet.""" self._access_token = accesstoken.AccessToken( authorization_server='https://authn.sd00.worldcat.org/oauth2', grant_type='authorization_code', options={ 'scope': ['WMS_NCIP', 'WMS_ACQ'], 'authenticating_institution_id': '128807', 'context_institution_id': '128808', 'redirect_uri': 'https://localhost:8000/auth/', 'code': 'unknown' }) """Assume authentication has occured and these parameters are now filled in.""" self._access_token.expires_at = '2014-04-08 13:38:29Z' self._access_token.expires_in = 1198 self._access_token.access_token_string = 'tk_TBHrsDbSrWW1oS7d3gZr7NJb7PokyOFlf0pr' self._access_token.type = 'bearer' self._access_token.refresh_token = refreshtoken.RefreshToken( tokenValue='rt_25fXauhJC09E4kwFxcf4TREkTnaRYWHgJA0W', expires_in=1199, expires_at='2014-03-13 15:44:59Z') self._my_access_token_formatter = access_token_formatter.AccessTokenFormatter( access_token=self._access_token)
def testCreateAccessToken(self): self.assertEqual(self._my_access_token.scope, ['WMS_NCIP', 'WMS_ACQ']) self.assertEqual(self._my_access_token.authenticating_institution_id, '128807') self.assertEqual(self._my_access_token.context_institution_id, '128808') self.assertEqual(self._my_access_token.redirect_uri, 'ncip://testapp') self.assertEqual(self._my_access_token.refresh_token.refresh_token, 'rt_25fXauhJC09E4kwFxcf4TREkTnaRYWHgJA0W') self.assertEqual(self._my_access_token.code, 'unknown') with self.assertRaises(accesstoken.InvalidGrantType): accesstoken.AccessToken( authorization_server=self._authorization_server) # Tests to make sure there are no missing parameters for authorization_code with self.assertRaises(accesstoken.NoOptionsPassed): accesstoken.AccessToken( authorization_server=self._authorization_server, grant_type='authorization_code', options={}) with self.assertRaises(accesstoken.RequiredOptionsMissing): accesstoken.AccessToken( authorization_server=self._authorization_server, grant_type='authorization_code', options={ 'authenticating_institution_id': '', 'context_institution_id': '' }) with self.assertRaises(accesstoken.RequiredOptionsMissing): accesstoken.AccessToken( authorization_server=self._authorization_server, grant_type='authorization_code', options={ 'code': '', 'context_institution_id': '' }) with self.assertRaises(accesstoken.RequiredOptionsMissing): accesstoken.AccessToken( authorization_server=self._authorization_server, grant_type='authorization_code', options={ 'code': '', 'authenticating_institution_id': '' }) # Tests to make sure there are no missing parameters for client_credentials with self.assertRaises(accesstoken.RequiredOptionsMissing): accesstoken.AccessToken( authorization_server=self._authorization_server, grant_type='client_credentials', options={ 'refresh_token': '', 'context_institution_id': '', 'scope': '' }) with self.assertRaises(accesstoken.RequiredOptionsMissing): accesstoken.AccessToken( authorization_server=self._authorization_server, grant_type='refresh_token', options={ 'client_credentials': '', 'authenticating_institution_id': '', 'scope': '' }) with self.assertRaises(accesstoken.RequiredOptionsMissing): accesstoken.AccessToken( authorization_server=self._authorization_server, grant_type='client_credentials', options={ 'refresh_token': '', 'authenticating_institution_id': '', 'context_institution_id': '' }) # Tests to make sure there are no missing parameters for refresh_token with self.assertRaises(accesstoken.RequiredOptionsMissing): accesstoken.AccessToken( authorization_server=self._authorization_server, grant_type='refresh_token', options={ 'authenticating_institution_id': '', 'context_institution_id': '', 'scope': '' }) # Test that scope must be a list of scopes, not a string with self.assertRaises(accesstoken.RequiredOptionsMissing): accesstoken.AccessToken( authorization_server=self._authorization_server, grant_type='authorization_code', options={ 'code': '', 'redirect_uri': '', 'authenticating_institution_id': '', 'context_institution_id': '', 'scope': 'WMS_ACQ' })