Пример #1
0
 def test_client_init(self, mock_post):
     
     basic = authentication.Basic(username = "******",
                                  password = "******"
                                 )
     
     oauth2 = authentication.OAuth2(api_key = 'thisisnotanapikey')
     
     self.logger.info('test_client_init')
     
     got_exception = False
     
     try:
         client = cl.Client(authentication = oauth2)
     except Exception as ex:
         got_exception = True
     
     self.assertFalse(got_exception)
     self.assertEqual(client.host, "https://pairs.res.ibm.com")
     self.assertEqual(client.headers, constants.CLIENT_JSON_HEADER)
     self.assertEqual(client.authentication.api_key, "thisisnotanapikey")
     self.assertEqual(client.authentication.jwt_token, "thisisnotanaccesstoken")
     self.assertEqual(client.body, None)
     
     self.logger.info('test_client_init: set attributes')
     
     got_exception = False
     
     try:
         client.host                     = "https://pairs.res.ibm.com"
         client.headers                  = constants.CLIENT_JSON_HEADER
         client.authentication.api_key   = "thisisnotanapikey"
         client.authentication.jwt_token = "thisisnotanaccesstoken"
     except Exception as ex:
         got_exception = True
         
     self.assertFalse(got_exception)
    def test_refresh_auth_token(self, mock_post):

        #
        self.logger.info('test_refresh_auth_token')

        self.logger.info('test_refresh_auth_token: 200, success')

        got_exception = False
        try:
            credentials = authentication.OAuth2(api_key='thisisnotanapikey')
            credentials.refresh_auth_token()
        except:
            got_exception = True

        self.assertFalse(got_exception)

        self.assertEqual(credentials.jwt_token, "thisisnotanewaccesstoken")
        self.assertEqual(credentials.oauth2_return.access_token,
                         "thisisnotanewaccesstoken")
        self.assertEqual(credentials.oauth2_return.expires_in, 3600)
        self.assertEqual(credentials.oauth2_return.token_type, "Bearer")
        self.assertEqual(credentials.oauth2_return.refresh_token,
                         "thisisnotanewrefreshtoken")
        self.assertEqual(credentials.oauth2_return.scope, "access:A B C D")

        #
        self.logger.info('test_refresh_auth_token: 200, invalid_grant')

        credentials = authentication.OAuth2(api_key='thisisnotanapikey')

        got_exception = False
        try:
            credentials.oauth2_return.refresh_token = "wrong-refresh-token"
            credentials.refresh_auth_token()
            self.assertEqual(credentials.oauth2_return.error, "invalid_grant")
        except Exception as ex:
            got_exception = True

        self.assertTrue(got_exception)

        #
        self.logger.info('test_refresh_auth_token: 200, wrong client id')

        credentials = authentication.OAuth2(api_key='thisisnotanapikey')

        got_exception = False
        try:
            credentials.client_id = 'wrong-client-id'
            credentials.refresh_auth_token()
            self.assertEqual(credentials.oauth2_return.error, "invalid_client")
        except Exception as ex:
            got_exception = True

        self.assertTrue(got_exception)

        #
        self.logger.info('test_refresh_auth_token: 404, wrong endpoint')

        credentials = authentication.OAuth2(api_key='thisisnotanapikey')

        got_exception = False
        try:
            credentials.endpoint = 'wrong.end.point'
            credentials.refresh_auth_token()
        except Exception as ex:
            got_exception = True

        self.assertTrue(got_exception)
    def test_oauth2_init(self, mock_post):
        self.logger.info('test_oauth2_init')

        got_exception = False

        try:
            oauth2 = authentication.OAuth2(api_key='thisisnotanapikey')
            oauth2.host = "https://pairs.res.ibm.com"
            oauth2.username = "******"
            oauth2.api_key = "thisisnotanapikey"
            oauth2.api_key_file = "auth/oauth2.txt"
            oauth2.client_id = "ibm-pairs"
            oauth2.endpoint = "auth-b2b-twc.ibm.com"
            oauth2.jwt_token = "thisisnotajwttoken"
        except Exception as ex:
            got_exception = True

        self.assertFalse(got_exception)
        self.assertEqual(oauth2.host, "pairs.res.ibm.com")
        self.assertEqual(oauth2.username, "*****@*****.**")
        self.assertEqual(oauth2.api_key, "thisisnotanapikey")
        cwd = os.getcwd()
        self.assertEqual(oauth2.api_key_file, cwd + "/auth/oauth2.txt")
        self.assertEqual(oauth2.client_id, "ibm-pairs")
        self.assertEqual(oauth2.endpoint, "auth-b2b-twc.ibm.com")
        self.assertEqual(oauth2.jwt_token, "thisisnotajwttoken")

        self.logger.info('test_oauth2_init: file find api_key')

        self.logger.info('writing \'oauth2-unittest.txt\'')
        f = open("oauth2-unittest.txt", "a")
        f.write("pairs.res.ibm.com:[email protected]:thisisnotanapikey")
        f.close()

        credentials2 = None

        try:
            credentials2 = authentication.OAuth2(
                api_key_file='oauth2-unittest.txt',
                username='******')
        except Exception as ex:
            got_exception = True

        self.assertFalse(got_exception)
        self.assertEqual(credentials2.jwt_token, "thisisnotanaccesstoken")

        self.logger.info('removing \'oauth2-unittest.txt\'')
        if os.path.isfile(os.path.join(os.getcwd(), 'oauth2-unittest.txt')):
            os.remove(os.path.join(os.getcwd(), 'oauth2-unittest.txt'))

        self.logger.info('test_oauth2_init: api_key is present')

        credentials3 = None

        try:
            credentials3 = authentication.OAuth2(api_key='thisisnotanapikey')
        except Exception as ex:
            got_exception = True

        self.assertFalse(got_exception)
        self.assertEqual(credentials3.jwt_token, "thisisnotanaccesstoken")

        self.logger.info('test_oauth2_init: no api_key')

        credentials4 = None

        try:
            credentials4 = authentication.OAuth2(
                api_key='thisisnotavalidapikey')
        except Exception as ex:
            self.assertEqual(
                str(ex),
                "AUTHENTICATION FAILED: A JWT token could not be gathered from the provided attributes."
            )
            got_exception = True

        self.assertTrue(got_exception)
Пример #4
0
    def test_refresh_auth_token(self, mock_post):

        #
        self.logger.info('test_refresh_auth_token')

        self.logger.info('test: 200, success')

        credentials = authentication.OAuth2()

        credentials.get_auth_token(
            api_key='PABUdniu33fu2NIODSNOmkfenw80hn0hdsh9NOSDInd')

        credentials.refresh_auth_token()

        self.assertEqual(credentials.jwt_token,
                         "HNanna8s8hx8nsnDNXS2NONKNW4QKNXCNWnc8whWH0cwh8cH")
        self.assertEqual(credentials.oauth2_return.access_token,
                         "HNanna8s8hx8nsnDNXS2NONKNW4QKNXCNWnc8whWH0cwh8cH")
        self.assertEqual(credentials.oauth2_return.expires_in, 3600)
        self.assertEqual(credentials.oauth2_return.token_type, "Bearer")
        self.assertEqual(credentials.oauth2_return.refresh_token,
                         "J-xaooxmPWXopmcpqm0h80h8nicwonkoxnx0qxhj887")
        self.assertEqual(credentials.oauth2_return.scope, "access:A B C D")

        #
        self.logger.info('test: 200, invalid_grant')

        credentials = authentication.OAuth2()

        credentials.get_auth_token(
            api_key='PABUdniu33fu2NIODSNOmkfenw80hn0hdsh9NOSDInd')

        got_exception = False
        try:
            credentials.oauth2_return.refresh_token = "wrong-refresh-token"
            credentials.refresh_auth_token()
            self.assertEqual(credentials.oauth2_return.error, "invalid_grant")
        except Exception as ex:
            got_exception = True

        self.assertTrue(got_exception)

        #
        self.logger.info('test: 200, wrong client id')

        credentials = authentication.OAuth2()

        credentials.get_auth_token(
            api_key='PABUdniu33fu2NIODSNOmkfenw80hn0hdsh9NOSDInd')

        got_exception = False
        try:
            credentials.client_id = 'wrong-client-id'
            credentials.refresh_auth_token()
            self.assertEqual(credentials.oauth2_return.error, "invalid_client")
        except Exception as ex:
            got_exception = True

        self.assertTrue(got_exception)

        #
        self.logger.info('test: 404, wrong endpoint')

        credentials = authentication.OAuth2()

        credentials.get_auth_token(
            api_key='PABUdniu33fu2NIODSNOmkfenw80hn0hdsh9NOSDInd')

        got_exception = False
        try:
            credentials.endpoint = 'wrong.end.point'
            credentials.refresh_auth_token()
        except Exception as ex:
            got_exception = True

        self.assertTrue(got_exception)
Пример #5
0
    def test_get_auth_token(self, mock_post):

        #
        self.logger.info('test_get_auth_token')

        self.logger.info('test: 200, success')

        credentials = authentication.OAuth2()

        credentials.get_auth_token(
            api_key='PABUdniu33fu2NIODSNOmkfenw80hn0hdsh9NOSDInd')

        self.assertEqual(credentials.jwt_token,
                         "MDASPcnivo8enPCSnadPNC8PNC8D9PNV8DPNVDS8DNVPASCO")
        self.assertEqual(credentials.oauth2_return.access_token,
                         "MDASPcnivo8enPCSnadPNC8PNC8D9PNV8DPNVDS8DNVPASCO")
        self.assertEqual(credentials.oauth2_return.expires_in, 3600)
        self.assertEqual(credentials.oauth2_return.token_type, "Bearer")
        self.assertEqual(credentials.oauth2_return.refresh_token,
                         "8dh0oNDIWONIOWNM-NDNNIondini0_nidoidoimioJD")
        self.assertEqual(credentials.oauth2_return.scope, "access:A B C D")

        #
        self.logger.info('test: 200, invalid_grant')

        got_exception = False
        try:
            credentials.get_auth_token(api_key='incorrect_key')
            self.assertEqual(credentials.oauth2_return.error, "invalid_grant")
        except Exception as ex:
            got_exception = True

        self.assertTrue(got_exception)

        #
        self.logger.info('test: 200, wrong client id')

        got_exception = False
        try:
            credentials.client_id = 'wrong-client-id'
            credentials.get_auth_token(
                api_key='PABUdniu33fu2NIODSNOmkfenw80hn0hdsh9NOSDInd')
            self.assertEqual(credentials.oauth2_return.error, "invalid_client")
        except Exception as ex:
            got_exception = True

        self.assertTrue(got_exception)

        credentials.client_id = 'ibm-pairs'

        #
        self.logger.info('test: 404, wrong endpoint')

        got_exception = False
        try:
            credentials.endpoint = 'wrong.end.point'
            credentials.get_auth_token(
                api_key='PABUdniu33fu2NIODSNOmkfenw80hn0hdsh9NOSDInd')
        except Exception as ex:
            got_exception = True

        self.assertTrue(got_exception)

        credentials.endpoint = 'auth-b2b-twc.ibm.com'
Пример #6
0
 def test_client_delete(self, mock_post, mock_delete):
     self.logger.info('test_client_delete')
     
     basic = authentication.Basic(username = "******",
                                  password = "******"
                                 )
     
     oauth2 = authentication.OAuth2(api_key = 'thisisnotanapikey')
     
     got_exception = False
     
     try:
        client = cl.Client(authentication = oauth2)
     except Exception as ex:
         got_exception = True
     
     self.assertFalse(got_exception)
     
     got_exception2 = False
     
     self.assertEqual(client.authentication.jwt_token, "thisisnotanaccesstoken")
     
     self.logger.info('test_client_delete: expired token core')
     
     try:
         resp = client.delete(url  = "https://token.refresh")
     except:
         got_exception2 = True
 
     self.assertFalse(got_exception2)
     self.assertEqual(resp.json(), r'''{"message":"success"}''')
     self.assertEqual(client.authentication.jwt_token, "thisisnotanewaccesstoken")
     self.assertEqual(client.authentication.oauth2_return.access_token, "thisisnotanewaccesstoken")
     self.assertEqual(client.authentication.oauth2_return.refresh_token, "thisisnotanewrefreshtoken")
     
     self.logger.info('test_client_delete: expired token upload')
         
     got_exception3 = False
     
     try:
         oauth2_2 = authentication.OAuth2(api_key = 'thisisnotanapikey')
         client2 = cl.Client(authentication = oauth2_2)
         resp2 = client2.delete(url  = "https://token.refreshed")
     except:
         got_exception3 = True
 
     self.assertFalse(got_exception3)
     self.assertEqual(resp2.json(), r'''{"message":"success"}''')
     self.assertEqual(client.authentication.jwt_token, "thisisnotanewaccesstoken")
     self.assertEqual(client.authentication.oauth2_return.access_token, "thisisnotanewaccesstoken")
     self.assertEqual(client.authentication.oauth2_return.refresh_token, "thisisnotanewrefreshtoken")
     
     self.logger.info('test_client_delete: 404')
     
     got_exception4 = False
     
     try:
         resp3 = client.delete(url  = "https://token.refreshing")
     except:
         got_exception4 = True
 
     self.assertFalse(got_exception4)
     self.assertEqual(resp3.status_code, 404)
     self.assertEqual(resp3.json(), None)
     self.assertEqual(client.authentication.jwt_token, "thisisnotanewaccesstoken")
     self.assertEqual(client.authentication.oauth2_return.access_token, "thisisnotanewaccesstoken")
     self.assertEqual(client.authentication.oauth2_return.refresh_token, "thisisnotanewrefreshtoken")