コード例 #1
0
        public void Test_WhenFileNotExists_Throws_CredentialUnavailableException()
        {
            var credential = new TokenFileCredential();
            var exception  = Assert.ThrowsException <CredentialUnavailableException>(() =>
                                                                                     credential.GetToken(new TokenRequestContext(new string[] { _azureQuantumScope }), CancellationToken.None));

            Assert.AreEqual($"Token file at {_tokenFilePath} does not exist.", exception.Message);
        }
コード例 #2
0
        public void Test_WhenInvalidJson_Throws_CredentialUnavailableException()
        {
            string notJson    = "invalid json";
            var    credential = new TokenFileCredential(new FileSystemTestImpl(notJson));
            var    exception  = Assert.ThrowsException <CredentialUnavailableException>(() =>
                                                                                        credential.GetToken(new TokenRequestContext(new string[] { _azureQuantumScope }), CancellationToken.None));

            Assert.AreEqual("Failed to parse token file: Invalid JSON.", exception.Message);
        }
コード例 #3
0
        public void Test_WhenFileNotSet_Throws_CredentialUnavailableException()
        {
            Environment.SetEnvironmentVariable("AZURE_QUANTUM_TOKEN_FILE", null);
            var credential = new TokenFileCredential();

            var exception = Assert.ThrowsException <CredentialUnavailableException>(() =>
                                                                                    credential.GetToken(new TokenRequestContext(new string[] { _azureQuantumScope }), CancellationToken.None));

            Assert.AreEqual("Token file location not set.", exception.Message);
        }
コード例 #4
0
        public void Test_WhenMissingExpiresOnField_Throws_CredentialUnavailableException()
        {
            string content = @"{
              ""access_token"": ""fake_token""
            }";

            var credential = new TokenFileCredential(new FileSystemTestImpl(content));
            var exception  = Assert.ThrowsException <CredentialUnavailableException>(() =>
                                                                                     credential.GetToken(new TokenRequestContext(new string[] { _azureQuantumScope }), CancellationToken.None));

            Assert.AreEqual("Failed to parse token file: Missing expected value: 'expires_on'", exception.Message);
        }
コード例 #5
0
        public void Test_WhenExpiredToken_Throws_CredentialUnavailableException()
        {
            string content = @"{
              ""access_token"": ""fake_token"",
              ""expires_on"": 1628543125086
            }";

            var credential = new TokenFileCredential(new FileSystemTestImpl(content));
            var exception  = Assert.ThrowsException <CredentialUnavailableException>(() =>
                                                                                     credential.GetToken(new TokenRequestContext(new string[] { _azureQuantumScope }), CancellationToken.None));

            Assert.AreEqual("Token already expired at 2021-08-09 21:05:25Z", exception.Message);
        }
コード例 #6
0
        public async Task Test_WhenTokenIsValid_Returns_AccessToken()
        {
            var    expiresOn = DateTimeOffset.UtcNow.AddHours(1);
            string content   = $@"{{
              ""access_token"": ""fake_token"",
              ""expires_on"": {expiresOn.ToUnixTimeMilliseconds()}
            }}";

            var credential = new TokenFileCredential(new FileSystemTestImpl(content));
            var token      = await credential.GetTokenAsync(new TokenRequestContext(new string[] { _azureQuantumScope }), CancellationToken.None);

            Assert.AreEqual("fake_token", token.Token);
            Assert.AreEqual(expiresOn.ToUnixTimeMilliseconds(), token.ExpiresOn.ToUnixTimeMilliseconds());
        }