|
| 1 | +package identity |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | +) |
| 8 | + |
| 9 | +func TestAuthorityConfiguration(t *testing.T) { |
| 10 | + t.Parallel() |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + authorityType string |
| 14 | + tenantID string |
| 15 | + authority string |
| 16 | + expected string |
| 17 | + expectError bool |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "Default Authority", |
| 21 | + authorityType: AuthorityTypeDefault, |
| 22 | + expected: "https://login.microsoftonline.com/common", |
| 23 | + expectError: false, |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "Multi-Tenant Authority", |
| 27 | + authorityType: AuthorityTypeMultiTenant, |
| 28 | + tenantID: "12345", |
| 29 | + expected: "https://login.microsoftonline.com/12345", |
| 30 | + expectError: false, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "Custom Authority", |
| 34 | + authorityType: AuthorityTypeCustom, |
| 35 | + authority: "https://custom-authority.com", |
| 36 | + expected: "https://custom-authority.com", |
| 37 | + expectError: false, |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "Invalid Authority Type", |
| 41 | + authorityType: "invalid", |
| 42 | + expectError: true, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "Missing Tenant ID for Multi-Tenant", |
| 46 | + authorityType: AuthorityTypeMultiTenant, |
| 47 | + expectError: true, |
| 48 | + }, |
| 49 | + { |
| 50 | + name: "Missing Authority for Custom", |
| 51 | + authorityType: AuthorityTypeCustom, |
| 52 | + expectError: true, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "Default Authority Type with Tenant ID", |
| 56 | + authorityType: AuthorityTypeDefault, |
| 57 | + tenantID: "12345", |
| 58 | + expected: "https://login.microsoftonline.com/common", |
| 59 | + expectError: false, |
| 60 | + }, |
| 61 | + } |
| 62 | + |
| 63 | + for _, test := range tests { |
| 64 | + t.Run(test.name, func(t *testing.T) { |
| 65 | + ac := AuthorityConfiguration{ |
| 66 | + AuthorityType: test.authorityType, |
| 67 | + TenantID: test.tenantID, |
| 68 | + Authority: test.authority, |
| 69 | + } |
| 70 | + result, err := ac.getAuthority() |
| 71 | + if test.expectError { |
| 72 | + assert.Error(t, err) |
| 73 | + } else { |
| 74 | + assert.NoError(t, err) |
| 75 | + assert.Equal(t, test.expected, result) |
| 76 | + } |
| 77 | + }) |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func TestAuthorityConfigurationDefault(t *testing.T) { |
| 82 | + t.Parallel() |
| 83 | + ac := AuthorityConfiguration{} |
| 84 | + result, err := ac.getAuthority() |
| 85 | + assert.NoError(t, err) |
| 86 | + assert.Equal(t, "https://login.microsoftonline.com/common", result) |
| 87 | +} |
| 88 | + |
| 89 | +func TestAuthorityConfigurationMultiTenant(t *testing.T) { |
| 90 | + t.Parallel() |
| 91 | + ac := AuthorityConfiguration{ |
| 92 | + AuthorityType: AuthorityTypeMultiTenant, |
| 93 | + TenantID: "12345", |
| 94 | + } |
| 95 | + result, err := ac.getAuthority() |
| 96 | + assert.NoError(t, err) |
| 97 | + assert.Equal(t, "https://login.microsoftonline.com/12345", result) |
| 98 | +} |
| 99 | + |
| 100 | +func TestAuthorityConfigurationCustom(t *testing.T) { |
| 101 | + t.Parallel() |
| 102 | + ac := AuthorityConfiguration{ |
| 103 | + AuthorityType: AuthorityTypeCustom, |
| 104 | + Authority: "https://custom-authority.com", |
| 105 | + } |
| 106 | + result, err := ac.getAuthority() |
| 107 | + assert.NoError(t, err) |
| 108 | + assert.Equal(t, "https://custom-authority.com", result) |
| 109 | +} |
| 110 | + |
| 111 | +func TestAuthorityConfigurationInvalid(t *testing.T) { |
| 112 | + t.Parallel() |
| 113 | + ac := AuthorityConfiguration{ |
| 114 | + AuthorityType: "invalid", |
| 115 | + } |
| 116 | + result, err := ac.getAuthority() |
| 117 | + assert.Error(t, err) |
| 118 | + assert.Equal(t, "", result) |
| 119 | +} |
| 120 | + |
| 121 | +func TestAuthorityConfigurationMissingTenantID(t *testing.T) { |
| 122 | + t.Parallel() |
| 123 | + ac := AuthorityConfiguration{ |
| 124 | + AuthorityType: AuthorityTypeMultiTenant, |
| 125 | + } |
| 126 | + result, err := ac.getAuthority() |
| 127 | + assert.Error(t, err) |
| 128 | + assert.Equal(t, "", result) |
| 129 | +} |
| 130 | + |
| 131 | +func TestAuthorityConfigurationMissingAuthority(t *testing.T) { |
| 132 | + t.Parallel() |
| 133 | + ac := AuthorityConfiguration{ |
| 134 | + AuthorityType: AuthorityTypeCustom, |
| 135 | + } |
| 136 | + result, err := ac.getAuthority() |
| 137 | + assert.Error(t, err) |
| 138 | + assert.Equal(t, "", result) |
| 139 | +} |
| 140 | + |
| 141 | +func TestAuthorityConfigurationDefaultAuthorityType(t *testing.T) { |
| 142 | + t.Parallel() |
| 143 | + ac := AuthorityConfiguration{ |
| 144 | + TenantID: "12345", |
| 145 | + } |
| 146 | + result, err := ac.getAuthority() |
| 147 | + assert.NoError(t, err) |
| 148 | + assert.Equal(t, "https://login.microsoftonline.com/common", result) |
| 149 | +} |
| 150 | + |
| 151 | +func TestAuthorityConfigurationDefaultAuthorityTypeWithTenantID(t *testing.T) { |
| 152 | + t.Parallel() |
| 153 | + ac := AuthorityConfiguration{ |
| 154 | + AuthorityType: AuthorityTypeDefault, |
| 155 | + TenantID: "12345", |
| 156 | + } |
| 157 | + result, err := ac.getAuthority() |
| 158 | + assert.NoError(t, err) |
| 159 | + assert.Equal(t, "https://login.microsoftonline.com/common", result) |
| 160 | +} |
0 commit comments