Пример #1
0
    def test_get_auth_url_state_auto_generated(self, get_timestamp_mock,
                                               sign_params_mock, uuid_mock):
        expected_timestamp = '2015.11.02 09:37:16 +0000'

        expected_state = 'SOME-STATE'

        uuid_mock.uuid4.return_value = expected_state

        unsigned_params = copy.copy(self.auth_url_params)
        unsigned_params.update({
            'timestamp': expected_timestamp,
            'state': expected_state,
        })

        signed_params = copy.copy(unsigned_params)
        signed_params.update({'client_secret': 'SECRET'})

        get_timestamp_mock.return_value = expected_timestamp
        sign_params_mock.return_value = signed_params

        url = self.esia_auth.get_auth_url()

        expected_url = 'https://esia-portal1.test.gosuslugi.ru/aas/oauth2/ac?access_type=offline&client_id=TEST_CLIENT_ID&client_secret=SECRET&redirect_uri=http%3A%2F%2Flocalhost%3A8000%2Fhandle_esia_code&response_type=code&scope=openid+http%3A%2F%2Fesia.gosuslugi.ru%2Fusr_inf&state=SOME-STATE&timestamp=2015.11.02+09%3A37%3A16+%2B0000'
        self.assertEqual(url, expected_url)

        get_timestamp_mock.assert_called_once_with()
        uuid_mock.uuid4.assert_called_once_with()

        sign_params_mock.assert_called_once_with(
            SameDict(unsigned_params),
            certificate_file=TEST_SETTINGS.certificate_file,
            private_key_file=TEST_SETTINGS.private_key_file)
Пример #2
0
    def test_complete_authorization_ok_with_validation(self,
                                                       get_timestamp_mock,
                                                       sign_params_mock,
                                                       make_request_mock):

        expected_oid = 1000323031
        expected_token = 'eyJhbGciOiJSUzI1NiIsInNidCI6ImFjY2VzcyIsInR5cCI6IkpXVCIsInZlciI6MX0.eyJleHAiOjE0NDY1MDM1MDQsInNjb3BlIjoiaHR0cDpcL1wvZXNpYS5nb3N1c2x1Z2kucnVcL3Vzcl9pbmY_b2lkPTEwMDAzMjMwMzEgb3BlbmlkIiwiaXNzIjoiaHR0cDpcL1wvZXNpYS5nb3N1c2x1Z2kucnVcLyIsIm5iZiI6MTQ0NjQ5OTkwNCwidXJuOmVzaWE6c2lkIjoiMDQ2MzZjNTVlY2FhYTJjYjc4NGM5NjhmZjJjMzE4NTlmODVjM2JmM2IyY2UxYTI4MzM0YjlhYjI4YzczMjk3OCIsInVybjplc2lhOnNial9pZCI6MTAwMDMyMzAzMSwiY2xpZW50X2lkIjoiU0VQQ0FQIiwiaWF0IjoxNDQ2NDk5OTA0fQ.LYBJTAj1mOI6Ldq7HInyi8IBN1o37McL9b8Z1b6GukaYliPNPNAZ6TVxpdn4BGdFuDtbNsKLe7bJvA0KHkVbKxNE73ZrLaI8mK9uOYVdgYxyOhKrzJ3pZee3Tzu19itTqdBLS_IRLjXj3jX4HLRCIRey09lS4AoYplB6GnZQX39XgPKNFSkP059ImA6tX-MJfQ_ZnbCdcIpm_i6YG6M1qbg4S9f1ArksDtuS6gzW7Ody-AAI31lDWXScycQDZ49TRNbJ23F2wY5Ws-bZkbKzUUF2JdokEgPJuWLw7GAX3IwUOrleVA57rR7Oc8P29xBt0RjFr57NfLn8TmFoziWyZw'

        expected_response_data = {
            "state": "b7062082-5493-409b-b51b-c7f788136a1c",
            "token_type": "Bearer",
            "expires_in": 3600,
            "refresh_token": "bb35e3ef-7da7-4300-bddb-4e0d4972345b",
            "id_token": "TOKENDATA",
            "access_token": expected_token,
        }
        make_request_mock.return_value = expected_response_data

        expected_timestamp = '2015.11.02 09:37:16 +0000'

        get_timestamp_mock.return_value = expected_timestamp

        code = 'eyJhbGciOiJSUzI1NiIsInNidCI6ImF1dGhvcml6YXRpb25fY29kZSIsInR5cCI6IkpXVCIsInZlciI6MX0.eyJhdXRoX3RpbWUiOjE0NDY0OTk3NTY2MDMsImF1dGhfbXRoZCI6IlBXRCIsImV4cCI6MTQ0Nzk0NjMxOTU5NCwic2NvcGUiOiJodHRwOlwvXC9lc2lhLmdvc3VzbHVnaS5ydVwvdXNyX2luZj9vaWQ9MTAwMDMyMzAzMSBvcGVuaWQiLCJpc3MiOiJodHRwOlwvXC9lc2lhLmdvc3VzbHVnaS5ydVwvIiwibmJmIjoxNDQ2NDk5NzU5LCJ1cm46ZXNpYTpjbGllbnQ6c3RhdGUiOiJiNzA2MjA4Mi01NDkzLTQwOWItYjUxYi1jN2Y3ODgxMzZhMWMiLCJ1cm46ZXNpYTpzaWQiOiIwNDYzNmM1NWVjYWFhMmNiNzg0Yzk2OGZmMmMzMTg1OWY4NWMzYmYzYjJjZTFhMjgzMzRiOWFiMjhjNzMyOTc4IiwicGFyYW1zIjp7InJlbW90ZV9pcCI6Ijk1LjI3LjgzLjEzOCIsInVzZXJfYWdlbnQiOiJNb3ppbGxhXC81LjAgKFgxMTsgTGludXggeDg2XzY0KSBBcHBsZVdlYktpdFwvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lXC80Ni4wLjI0OTAuODAgU2FmYXJpXC81MzcuMzYifSwidXJuOmVzaWE6c2JqIjp7InVybjplc2lhOnNiajplaWQiOjc0Mzc4NDgsInVybjplc2lhOnNiajpuYW0iOiJPSUQuMTAwMDMyMzAzMSIsInVybjplc2lhOnNiajpvaWQiOjEwMDAzMjMwMzEsInVybjplc2lhOnNiajp0eXAiOiJQIn0sImNsaWVudF9pZCI6IlNFUENBUCIsImlhdCI6MTQ0NjQ5OTc1OX0.Usz6bANHcdRsx4Pg_eqbaNsa9NmpXpUasx5NBakV1crnwo-nEsau19a0mvMVNr6QrS8vqcPEgfQCUecBjxCrcOlVl2qKpYlIYySLtWyCFgBHvuM9vsJBUPeIcKD3Ta_IFoDbofzHbzqJ61wB2Ckqf_erpo08BqVxiT3ZxuRv4iIozNJxgXLHHWbDGVQ6wymsUhiTIGcNfJhrItHtsyKhVlpAnwtY-I9Jm0YkRNe6pGkHdrGyVFClMmV8-HkkFZq6VBHpPqkcK1hl_cMQdHMqEhySuw1oojOrTC-0jZNjuzSacJHXVmrpI4k7F3rDorlziflBpx00m88ox4lA6BAfmw'
        state = 'b7062082-5493-409b-b51b-c7f788136a1c'

        unsigned_params = {
            'client_id': TEST_SETTINGS.esia_client_id,
            'code': code,
            'grant_type': 'authorization_code',
            'redirect_uri': TEST_SETTINGS.redirect_uri,
            'timestamp': expected_timestamp,
            'token_type': 'Bearer',
            'scope': TEST_SETTINGS.esia_scope,
            'state': state,
        }
        signed_params = copy.copy(unsigned_params)
        signed_params.update({'client_secret': 'SECRET'})

        sign_params_mock.return_value = signed_params

        esia_auth = EsiaAuth(TEST_SETTINGS)
        parse_token_mock = create_autospec(esia_auth._parse_token)
        esia_auth._parse_token = parse_token_mock
        validate_token_mock = create_autospec(esia_auth._validate_token)
        validate_token_mock.return_value = {
            'urn:esia:sbj': {
                'urn:esia:sbj:oid': expected_oid
            }
        }
        esia_auth._validate_token = validate_token_mock

        result = esia_auth.complete_authorization(code,
                                                  state,
                                                  validate_token=True)

        self.assertIsInstance(result, EsiaInformationConnector)
        self.assertEqual(result.oid, expected_oid)
        self.assertEqual(result.token, expected_token)
        self.assertEqual(result.settings, esia_auth.settings)

        get_timestamp_mock.assert_called_once_with()
        sign_params_mock.assert_called_once_with(
            SameDict(unsigned_params),
            certificate_file=TEST_SETTINGS.certificate_file,
            private_key_file=TEST_SETTINGS.private_key_file)

        make_request_mock.assert_called_once_with(url="{0}{1}".format(
            TEST_SETTINGS.esia_service_url, EsiaAuth._TOKEN_EXCHANGE_URL),
                                                  method='POST',
                                                  data=signed_params)
        validate_token_mock.assert_called_once_with('TOKENDATA')
        self.assertFalse(parse_token_mock.called)