|
| 1 | +// Copyright (c) Duende Software. All rights reserved. |
| 2 | +// See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +using Duende.IdentityServer.Licensing.V2.Diagnostics.DiagnosticEntries; |
| 5 | +using Duende.IdentityServer.Models; |
| 6 | + |
| 7 | +namespace IdentityServer.UnitTests.Licensing.V2.DiagnosticEntries; |
| 8 | + |
| 9 | +public class TokenIssueCountDiagnosticEntryTests |
| 10 | +{ |
| 11 | + private readonly TokenIssueCountDiagnosticEntry _subject = new(); |
| 12 | + |
| 13 | + [Fact] |
| 14 | + public async Task Should_Count_JwtAccessToken() |
| 15 | + { |
| 16 | + IssueToken(true, AccessTokenType.Jwt, false, ProofType.None, false); |
| 17 | + |
| 18 | + var result = await DiagnosticEntryTestHelper.WriteEntryToJson(_subject); |
| 19 | + |
| 20 | + result.RootElement.GetProperty("TokenIssueCounts").GetProperty("Jwt").GetInt64().ShouldBe(1); |
| 21 | + } |
| 22 | + |
| 23 | + [Fact] |
| 24 | + public async Task Should_Count_JwtReferenceToken() |
| 25 | + { |
| 26 | + IssueToken(true, AccessTokenType.Reference, false, ProofType.None, false); |
| 27 | + |
| 28 | + var result = await DiagnosticEntryTestHelper.WriteEntryToJson(_subject); |
| 29 | + |
| 30 | + result.RootElement.GetProperty("TokenIssueCounts").GetProperty("Reference").GetInt64().ShouldBe(1); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public async Task Should_Count_JwtDPoPToken() |
| 35 | + { |
| 36 | + IssueToken(true, AccessTokenType.Jwt, false, ProofType.DPoP, false); |
| 37 | + |
| 38 | + var result = await DiagnosticEntryTestHelper.WriteEntryToJson(_subject); |
| 39 | + |
| 40 | + result.RootElement.GetProperty("TokenIssueCounts").GetProperty("JwtPoPDPoP").GetInt64().ShouldBe(1); |
| 41 | + } |
| 42 | + |
| 43 | + [Fact] |
| 44 | + public async Task Should_Count_ReferenceDPoPToken() |
| 45 | + { |
| 46 | + IssueToken(true, AccessTokenType.Reference, false, ProofType.DPoP, false); |
| 47 | + |
| 48 | + var result = await DiagnosticEntryTestHelper.WriteEntryToJson(_subject); |
| 49 | + |
| 50 | + result.RootElement.GetProperty("TokenIssueCounts").GetProperty("ReferencePoPDPoP").GetInt64().ShouldBe(1); |
| 51 | + } |
| 52 | + |
| 53 | + [Fact] |
| 54 | + public async Task Should_Count_JwtMTlsToken() |
| 55 | + { |
| 56 | + IssueToken(true, AccessTokenType.Jwt, false, ProofType.ClientCertificate, false); |
| 57 | + |
| 58 | + var result = await DiagnosticEntryTestHelper.WriteEntryToJson(_subject); |
| 59 | + |
| 60 | + result.RootElement.GetProperty("TokenIssueCounts").GetProperty("JwtPoPmTLS").GetInt64().ShouldBe(1); |
| 61 | + } |
| 62 | + |
| 63 | + [Fact] |
| 64 | + public async Task Should_Count_ReferenceMTlsToken() |
| 65 | + { |
| 66 | + IssueToken(true, AccessTokenType.Reference, false, ProofType.ClientCertificate, false); |
| 67 | + |
| 68 | + var result = await DiagnosticEntryTestHelper.WriteEntryToJson(_subject); |
| 69 | + |
| 70 | + result.RootElement.GetProperty("TokenIssueCounts").GetProperty("ReferencePoPmTLS").GetInt64().ShouldBe(1); |
| 71 | + } |
| 72 | + |
| 73 | + [Fact] |
| 74 | + public async Task Should_Count_RefreshToken() |
| 75 | + { |
| 76 | + IssueToken(false, AccessTokenType.Jwt, true, ProofType.None, false); |
| 77 | + |
| 78 | + var result = await DiagnosticEntryTestHelper.WriteEntryToJson(_subject); |
| 79 | + |
| 80 | + result.RootElement.GetProperty("TokenIssueCounts").GetProperty("Refresh").GetInt64().ShouldBe(1); |
| 81 | + } |
| 82 | + |
| 83 | + [Fact] |
| 84 | + public async Task Should_Count_IdToken() |
| 85 | + { |
| 86 | + IssueToken(false, AccessTokenType.Jwt, false, ProofType.None, true); |
| 87 | + |
| 88 | + var result = await DiagnosticEntryTestHelper.WriteEntryToJson(_subject); |
| 89 | + |
| 90 | + result.RootElement.GetProperty("TokenIssueCounts").GetProperty("Id").GetInt64().ShouldBe(1); |
| 91 | + } |
| 92 | + |
| 93 | + [Fact] |
| 94 | + public async Task Should_Handle_Multiple_Token_Types() |
| 95 | + { |
| 96 | + IssueToken(true, AccessTokenType.Jwt, true, ProofType.None, false); |
| 97 | + IssueToken(true, AccessTokenType.Jwt, false, ProofType.DPoP, false); |
| 98 | + |
| 99 | + var result = await DiagnosticEntryTestHelper.WriteEntryToJson(_subject); |
| 100 | + |
| 101 | + var tokenIssueCounts = result.RootElement.GetProperty("TokenIssueCounts"); |
| 102 | + tokenIssueCounts.GetProperty("Jwt").GetInt64().ShouldBe(1); |
| 103 | + tokenIssueCounts.GetProperty("JwtPoPDPoP").GetInt64().ShouldBe(1); |
| 104 | + tokenIssueCounts.GetProperty("Refresh").GetInt64().ShouldBe(1); |
| 105 | + } |
| 106 | + |
| 107 | + [Fact] |
| 108 | + public async Task Should_Ignore_Non_TokenIssued_Instruments() |
| 109 | + { |
| 110 | + Duende.IdentityServer.Telemetry.Metrics.TokenIssuedFailure("ClientId", "GrantType", null, "error"); |
| 111 | + |
| 112 | + var result = await DiagnosticEntryTestHelper.WriteEntryToJson(_subject); |
| 113 | + |
| 114 | + var tokenIssueCounts = result.RootElement.GetProperty("TokenIssueCounts"); |
| 115 | + tokenIssueCounts.GetProperty("Jwt").GetInt64().ShouldBe(0); |
| 116 | + tokenIssueCounts.GetProperty("Reference").GetInt64().ShouldBe(0); |
| 117 | + tokenIssueCounts.GetProperty("JwtPoPDPoP").GetInt64().ShouldBe(0); |
| 118 | + tokenIssueCounts.GetProperty("JwtPoPmTLS").GetInt64().ShouldBe(0); |
| 119 | + tokenIssueCounts.GetProperty("ReferencePoPDPoP").GetInt64().ShouldBe(0); |
| 120 | + tokenIssueCounts.GetProperty("ReferencePoPmTLS").GetInt64().ShouldBe(0); |
| 121 | + tokenIssueCounts.GetProperty("Refresh").GetInt64().ShouldBe(0); |
| 122 | + } |
| 123 | + |
| 124 | + private void IssueToken(bool accessTokenIssued, AccessTokenType accessTokenType, bool refreshTokenIssued, |
| 125 | + ProofType proofType, bool idTokenIssued) => |
| 126 | + Duende.IdentityServer.Telemetry.Metrics.TokenIssued("ClientId", "GrantType", null, accessTokenIssued, accessTokenType, refreshTokenIssued, proofType, idTokenIssued); |
| 127 | +} |
0 commit comments