|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using Xunit; |
| 4 | + |
| 5 | +namespace GitHub.Tests; |
| 6 | + |
| 7 | +public class GitHubAuthChallengeTests |
| 8 | +{ |
| 9 | + [Fact] |
| 10 | + public void GitHubAuthChallenge_FromHeaders_CaseInsensitive() |
| 11 | + { |
| 12 | + var headers = new[] |
| 13 | + { |
| 14 | + "BASIC REALM=\"GITHUB\"", |
| 15 | + "basic realm=\"github\"", |
| 16 | + "bAsIc ReAlM=\"gItHuB\"", |
| 17 | + }; |
| 18 | + |
| 19 | + IList<GitHubAuthChallenge> challenges = GitHubAuthChallenge.FromHeaders(headers); |
| 20 | + Assert.Equal(3, challenges.Count); |
| 21 | + |
| 22 | + foreach (var challenge in challenges) |
| 23 | + { |
| 24 | + Assert.Null(challenge.Domain); |
| 25 | + Assert.Null(challenge.Enterprise); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + [Fact] |
| 30 | + public void GitHubAuthChallenge_FromHeaders_MultipleRealms_ReturnsGitHubOnly() |
| 31 | + { |
| 32 | + var headers = new[] |
| 33 | + { |
| 34 | + "Basic realm=\"contoso\"", |
| 35 | + "Basic realm=\"GitHub\"", |
| 36 | + "Basic realm=\"fabrikam\"", |
| 37 | + }; |
| 38 | + |
| 39 | + IList<GitHubAuthChallenge> challenges = GitHubAuthChallenge.FromHeaders(headers); |
| 40 | + Assert.Single(challenges); |
| 41 | + |
| 42 | + Assert.Null(challenges[0].Domain); |
| 43 | + Assert.Null(challenges[0].Enterprise); |
| 44 | + } |
| 45 | + |
| 46 | + [Fact] |
| 47 | + public void GitHubAuthChallenge_FromHeaders_NoMatchingRealms_ReturnsEmpty() |
| 48 | + { |
| 49 | + var headers = new[] |
| 50 | + { |
| 51 | + "Basic realm=\"contoso\"", |
| 52 | + "Basic realm=\"fabrikam\"", |
| 53 | + "Basic realm=\"example\"", |
| 54 | + }; |
| 55 | + |
| 56 | + IList<GitHubAuthChallenge> challenges = GitHubAuthChallenge.FromHeaders(headers); |
| 57 | + Assert.Empty(challenges); |
| 58 | + } |
| 59 | + |
| 60 | + [Theory] |
| 61 | + [InlineData("Basic realm=\"GitHub\" enterprise_hint=\"contoso-corp\" domain_hint=\"contoso\"", "contoso", "contoso-corp")] |
| 62 | + [InlineData("Basic realm=\"GitHub\" domain_hint=\"contoso\"", "contoso", null)] |
| 63 | + [InlineData("Basic realm=\"GitHub\" enterprise_hint=\"contoso-corp\"", null, "contoso-corp")] |
| 64 | + [InlineData("Basic realm=\"GitHub\" domain_hint=\"fab\" enterprise_hint=\"fabirkamopensource\"", "fab", "fabirkamopensource")] |
| 65 | + [InlineData("Basic enterprise_hint=\"iana\" realm=\"GitHub\" domain_hint=\"example\"", "example", "iana")] |
| 66 | + [InlineData("Basic domain_hint=\"test\" enterprise_hint=\"test-inc\" realm=\"GitHub\"", "test", "test-inc")] |
| 67 | + public void GitHubAuthChallenge_FromHeaders_Hints_ReturnsWithHints(string header, string domain, string enterprise) |
| 68 | + { |
| 69 | + IList<GitHubAuthChallenge> challenges = GitHubAuthChallenge.FromHeaders(new[] { header }); |
| 70 | + Assert.Single(challenges); |
| 71 | + |
| 72 | + Assert.Equal(domain, challenges[0].Domain); |
| 73 | + Assert.Equal(enterprise, challenges[0].Enterprise); |
| 74 | + } |
| 75 | + |
| 76 | + [Fact] |
| 77 | + public void GitHubAuthChallenge_FromHeaders_EmptyHeaders_ReturnsEmpty() |
| 78 | + { |
| 79 | + string[] headers = Array.Empty<string>(); |
| 80 | + IList<GitHubAuthChallenge> challenges = GitHubAuthChallenge.FromHeaders(headers); |
| 81 | + Assert.Empty(challenges); |
| 82 | + } |
| 83 | + |
| 84 | + [Theory] |
| 85 | + [InlineData(null, false)] |
| 86 | + [InlineData("", false)] |
| 87 | + [InlineData(" ", false)] |
| 88 | + [InlineData("alice", true)] |
| 89 | + [InlineData("alice_contoso", false)] |
| 90 | + [InlineData("alice_CONTOSO", false)] |
| 91 | + [InlineData("alice_contoso_alt", false)] |
| 92 | + [InlineData("pj_nitin", true)] |
| 93 | + [InlineData("up_the_irons", true)] |
| 94 | + public void GitHubAuthChallenge_IsDomainMember_NoHint(string userName, bool expected) |
| 95 | + { |
| 96 | + var challenge = new GitHubAuthChallenge(); |
| 97 | + Assert.Equal(expected, challenge.IsDomainMember(userName)); |
| 98 | + } |
| 99 | + |
| 100 | + [Theory] |
| 101 | + [InlineData(null, false)] |
| 102 | + [InlineData("", false)] |
| 103 | + [InlineData(" ", false)] |
| 104 | + [InlineData("alice", false)] |
| 105 | + [InlineData("alice_contoso", true)] |
| 106 | + [InlineData("alice_CONTOSO", true)] |
| 107 | + [InlineData("alice_contoso_alt", false)] |
| 108 | + [InlineData("pj_nitin", false)] |
| 109 | + [InlineData("up_the_irons", false)] |
| 110 | + public void GitHubAuthChallenge_IsDomainMember_DomainHint(string userName, bool expected) |
| 111 | + { |
| 112 | + var realm = new GitHubAuthChallenge("contoso", "contoso-corp"); |
| 113 | + Assert.Equal(expected, realm.IsDomainMember(userName)); |
| 114 | + } |
| 115 | + |
| 116 | + [Fact] |
| 117 | + public void GitHubAuthChallenge_Equals_Null_ReturnsFalse() |
| 118 | + { |
| 119 | + var challenge = new GitHubAuthChallenge("contoso", "contoso-corp"); |
| 120 | + Assert.False(challenge.Equals(null)); |
| 121 | + } |
| 122 | + |
| 123 | + [Fact] |
| 124 | + public void GitHubAuthChallenge_Equals_SameInstance_ReturnsTrue() |
| 125 | + { |
| 126 | + var challenge = new GitHubAuthChallenge("contoso", "contoso-corp"); |
| 127 | + Assert.True(challenge.Equals(challenge)); |
| 128 | + } |
| 129 | + |
| 130 | + [Fact] |
| 131 | + public void GitHubAuthChallenge_Equals_DifferentInstance_ReturnsTrue() |
| 132 | + { |
| 133 | + var challenge1 = new GitHubAuthChallenge("contoso", "constoso-corp"); |
| 134 | + var challenge2 = new GitHubAuthChallenge("contoso", "constoso-corp"); |
| 135 | + Assert.True(challenge1.Equals(challenge2)); |
| 136 | + } |
| 137 | + |
| 138 | + [Fact] |
| 139 | + public void GitHubAuthChallenge_Equals_DifferentCase_ReturnsTrue() |
| 140 | + { |
| 141 | + var challenge1 = new GitHubAuthChallenge("contoso", "contoso-corp"); |
| 142 | + var challenge2 = new GitHubAuthChallenge("CONTOSO", "CONTOSO-CORP"); |
| 143 | + Assert.True(challenge1.Equals(challenge2)); |
| 144 | + } |
| 145 | + |
| 146 | + [Fact] |
| 147 | + public void GitHubAuthChallenge_Equals_DifferentShortCode_ReturnsFalse() |
| 148 | + { |
| 149 | + var challenge1 = new GitHubAuthChallenge("contoso", "constoso-corp"); |
| 150 | + var challenge2 = new GitHubAuthChallenge("fab", "fabrikamopensource"); |
| 151 | + Assert.False(challenge1.Equals(challenge2)); |
| 152 | + } |
| 153 | +} |
0 commit comments