|
| 1 | +// Copyright (c) Duende Software. All rights reserved. |
| 2 | +// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information. |
| 3 | + |
| 4 | +namespace Duende.AccessTokenManagement.Types; |
| 5 | + |
| 6 | +/// <summary> |
| 7 | +/// Tests for <see cref="RefreshToken.SetMaxLength"/>. These are isolated into a separate |
| 8 | +/// non-parallel collection because <see cref="RefreshToken.SetMaxLength"/> mutates static state |
| 9 | +/// that would affect other tests running concurrently. |
| 10 | +/// </summary> |
| 11 | +[Collection(nameof(RefreshTokenTests))] |
| 12 | +public class RefreshTokenSetMaxLengthTests : IDisposable |
| 13 | +{ |
| 14 | + public void Dispose() => |
| 15 | + // Reset to the default max length after each test so static state doesn't leak. |
| 16 | + RefreshToken.SetMaxLength(RefreshToken.MaxLength); |
| 17 | + |
| 18 | + [Fact] |
| 19 | + public void SetMaxLength_AllowsLargerTokens() |
| 20 | + { |
| 21 | + // Arrange |
| 22 | + var largeMaxLength = 16 * 1024; // 16 KB, e.g. for ADFS tokens |
| 23 | + var largeValue = new string('a', RefreshToken.MaxLength + 1); // Exceeds default, within new limit |
| 24 | + RefreshToken.SetMaxLength(largeMaxLength); |
| 25 | + |
| 26 | + // Act |
| 27 | + var result = RefreshToken.Parse(largeValue); |
| 28 | + |
| 29 | + // Assert |
| 30 | + result.ToString().ShouldBe(largeValue); |
| 31 | + } |
| 32 | + |
| 33 | + [Fact] |
| 34 | + public void SetMaxLength_StillRejectsTokensExceedingNewLimit() |
| 35 | + { |
| 36 | + // Arrange |
| 37 | + var newMaxLength = 8 * 1024; |
| 38 | + RefreshToken.SetMaxLength(newMaxLength); |
| 39 | + var tooLargeValue = new string('a', newMaxLength + 1); |
| 40 | + |
| 41 | + // Act & Assert |
| 42 | + var exception = Should.Throw<InvalidOperationException>(() => RefreshToken.Parse(tooLargeValue)); |
| 43 | + exception.Message.ShouldContain("exceeds maximum length"); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void SetMaxLength_TryParse_AllowsLargerTokens() |
| 48 | + { |
| 49 | + // Arrange |
| 50 | + var largeMaxLength = 16 * 1024; |
| 51 | + var largeValue = new string('a', RefreshToken.MaxLength + 1); |
| 52 | + RefreshToken.SetMaxLength(largeMaxLength); |
| 53 | + |
| 54 | + // Act |
| 55 | + var success = RefreshToken.TryParse(largeValue, out var result, out var errors); |
| 56 | + |
| 57 | + // Assert |
| 58 | + success.ShouldBeTrue(); |
| 59 | + result.ShouldNotBeNull(); |
| 60 | + result.ToString().ShouldBe(largeValue); |
| 61 | + errors.ShouldBeEmpty(); |
| 62 | + } |
| 63 | + |
| 64 | + [Fact] |
| 65 | + public void SetMaxLength_TryParse_StillRejectsTokensExceedingNewLimit() |
| 66 | + { |
| 67 | + // Arrange |
| 68 | + var newMaxLength = 8 * 1024; |
| 69 | + RefreshToken.SetMaxLength(newMaxLength); |
| 70 | + var tooLargeValue = new string('a', newMaxLength + 1); |
| 71 | + |
| 72 | + // Act |
| 73 | + var success = RefreshToken.TryParse(tooLargeValue, out var result, out var errors); |
| 74 | + |
| 75 | + // Assert |
| 76 | + success.ShouldBeFalse(); |
| 77 | + result.ShouldBeNull(); |
| 78 | + errors.ShouldNotBeEmpty(); |
| 79 | + errors[0].ShouldContain("exceeds maximum length"); |
| 80 | + } |
| 81 | + |
| 82 | + [Fact] |
| 83 | + public void SetMaxLength_CanReduceLimit() |
| 84 | + { |
| 85 | + // Arrange |
| 86 | + var smallerMaxLength = 100; |
| 87 | + RefreshToken.SetMaxLength(smallerMaxLength); |
| 88 | + var value = new string('a', smallerMaxLength + 1); |
| 89 | + |
| 90 | + // Act & Assert |
| 91 | + var exception = Should.Throw<InvalidOperationException>(() => RefreshToken.Parse(value)); |
| 92 | + exception.Message.ShouldContain("exceeds maximum length"); |
| 93 | + } |
| 94 | + |
| 95 | + [Fact] |
| 96 | + public void SetMaxLength_AtExactNewLimit_Succeeds() |
| 97 | + { |
| 98 | + // Arrange |
| 99 | + var newMaxLength = 8 * 1024; |
| 100 | + RefreshToken.SetMaxLength(newMaxLength); |
| 101 | + var value = new string('a', newMaxLength); |
| 102 | + |
| 103 | + // Act |
| 104 | + var result = RefreshToken.Parse(value); |
| 105 | + |
| 106 | + // Assert |
| 107 | + result.ToString().ShouldBe(value); |
| 108 | + } |
| 109 | + |
| 110 | + [Fact] |
| 111 | + public void SetMaxLength_Zero_ThrowsArgumentOutOfRangeException() => |
| 112 | + // Act & Assert |
| 113 | + Should.Throw<ArgumentOutOfRangeException>(() => RefreshToken.SetMaxLength(0)); |
| 114 | + |
| 115 | + [Fact] |
| 116 | + public void SetMaxLength_Negative_ThrowsArgumentOutOfRangeException() => |
| 117 | + // Act & Assert |
| 118 | + Should.Throw<ArgumentOutOfRangeException>(() => RefreshToken.SetMaxLength(-1)); |
| 119 | +} |
0 commit comments