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); }
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); }
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); }
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); }
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); }
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()); }