|
| 1 | +package model_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/tinyauthapp/tinyauth/internal/model" |
| 8 | + "github.com/tinyauthapp/tinyauth/internal/repository" |
| 9 | +) |
| 10 | + |
| 11 | +func TestContext(t *testing.T) { |
| 12 | + tests := []struct { |
| 13 | + description string |
| 14 | + context *model.UserContext |
| 15 | + run func(*model.UserContext) any |
| 16 | + expected any |
| 17 | + }{ |
| 18 | + { |
| 19 | + description: "IsAuthenticated returns true when Authenticated is true", |
| 20 | + context: &model.UserContext{Authenticated: true}, |
| 21 | + run: func(c *model.UserContext) any { return c.IsAuthenticated() }, |
| 22 | + expected: true, |
| 23 | + }, |
| 24 | + { |
| 25 | + description: "IsAuthenticated returns false when Authenticated is false", |
| 26 | + context: &model.UserContext{Authenticated: false}, |
| 27 | + run: func(c *model.UserContext) any { return c.IsAuthenticated() }, |
| 28 | + expected: false, |
| 29 | + }, |
| 30 | + { |
| 31 | + description: "IsLocal returns true when Provider is ProviderLocal", |
| 32 | + context: &model.UserContext{Provider: model.ProviderLocal}, |
| 33 | + run: func(c *model.UserContext) any { return c.IsLocal() }, |
| 34 | + expected: true, |
| 35 | + }, |
| 36 | + { |
| 37 | + description: "IsLocal returns false when Provider is not ProviderLocal", |
| 38 | + context: &model.UserContext{Provider: model.ProviderOAuth}, |
| 39 | + run: func(c *model.UserContext) any { return c.IsLocal() }, |
| 40 | + expected: false, |
| 41 | + }, |
| 42 | + { |
| 43 | + description: "IsOAuth returns true when Provider is ProviderOAuth", |
| 44 | + context: &model.UserContext{Provider: model.ProviderOAuth}, |
| 45 | + run: func(c *model.UserContext) any { return c.IsOAuth() }, |
| 46 | + expected: true, |
| 47 | + }, |
| 48 | + { |
| 49 | + description: "IsOAuth returns false when Provider is ProviderLocal", |
| 50 | + context: &model.UserContext{Provider: model.ProviderLocal}, |
| 51 | + run: func(c *model.UserContext) any { return c.IsOAuth() }, |
| 52 | + expected: false, |
| 53 | + }, |
| 54 | + { |
| 55 | + description: "IsLDAP returns true when Provider is ProviderLDAP", |
| 56 | + context: &model.UserContext{Provider: model.ProviderLDAP}, |
| 57 | + run: func(c *model.UserContext) any { return c.IsLDAP() }, |
| 58 | + expected: true, |
| 59 | + }, |
| 60 | + { |
| 61 | + description: "IsLDAP returns false when Provider is ProviderOAuth", |
| 62 | + context: &model.UserContext{Provider: model.ProviderOAuth}, |
| 63 | + run: func(c *model.UserContext) any { return c.IsLDAP() }, |
| 64 | + expected: false, |
| 65 | + }, |
| 66 | + { |
| 67 | + description: "IsBasicAuth returns true when Provider is ProviderBasicAuth", |
| 68 | + context: &model.UserContext{Provider: model.ProviderBasicAuth}, |
| 69 | + run: func(c *model.UserContext) any { return c.IsBasicAuth() }, |
| 70 | + expected: true, |
| 71 | + }, |
| 72 | + { |
| 73 | + description: "IsBasicAuth returns false when Provider is ProviderLocal", |
| 74 | + context: &model.UserContext{Provider: model.ProviderLocal}, |
| 75 | + run: func(c *model.UserContext) any { return c.IsBasicAuth() }, |
| 76 | + expected: false, |
| 77 | + }, |
| 78 | + { |
| 79 | + description: "NewFromSession local session without TOTP sets ProviderLocal and is authenticated", |
| 80 | + context: &model.UserContext{}, |
| 81 | + run: func(c *model.UserContext) any { |
| 82 | + got, _ := c.NewFromSession(&repository.Session{ |
| 83 | + Username: "alice", Email: "alice@example.com", Name: "Alice", |
| 84 | + Provider: "local", TotpPending: false, |
| 85 | + }) |
| 86 | + return got.Provider == model.ProviderLocal && got.Authenticated |
| 87 | + }, |
| 88 | + expected: true, |
| 89 | + }, |
| 90 | + { |
| 91 | + description: "NewFromSession local session with TOTP pending is not authenticated", |
| 92 | + context: &model.UserContext{}, |
| 93 | + run: func(c *model.UserContext) any { |
| 94 | + got, _ := c.NewFromSession(&repository.Session{ |
| 95 | + Username: "bob", Provider: "local", TotpPending: true, |
| 96 | + }) |
| 97 | + return got.Authenticated |
| 98 | + }, |
| 99 | + expected: false, |
| 100 | + }, |
| 101 | + { |
| 102 | + description: "NewFromSession ldap session sets ProviderLDAP and is authenticated", |
| 103 | + context: &model.UserContext{}, |
| 104 | + run: func(c *model.UserContext) any { |
| 105 | + got, _ := c.NewFromSession(&repository.Session{ |
| 106 | + Username: "carol", Email: "carol@example.com", Name: "Carol", |
| 107 | + Provider: "ldap", |
| 108 | + }) |
| 109 | + return got.Provider == model.ProviderLDAP && got.Authenticated |
| 110 | + }, |
| 111 | + expected: true, |
| 112 | + }, |
| 113 | + { |
| 114 | + description: "NewFromSession unknown provider defaults to ProviderOAuth", |
| 115 | + context: &model.UserContext{}, |
| 116 | + run: func(c *model.UserContext) any { |
| 117 | + got, _ := c.NewFromSession(&repository.Session{ |
| 118 | + Username: "dave", Provider: "github", |
| 119 | + OAuthGroups: "devs,admins", OAuthSub: "sub-123", OAuthName: "GitHub", |
| 120 | + }) |
| 121 | + return got.Provider |
| 122 | + }, |
| 123 | + expected: model.ProviderOAuth, |
| 124 | + }, |
| 125 | + { |
| 126 | + description: "GetUsername returns local username for ProviderLocal", |
| 127 | + context: &model.UserContext{ |
| 128 | + Provider: model.ProviderLocal, |
| 129 | + Local: &model.LocalContext{BaseContext: model.BaseContext{Username: "alice"}}, |
| 130 | + }, |
| 131 | + run: func(c *model.UserContext) any { return c.GetUsername() }, |
| 132 | + expected: "alice", |
| 133 | + }, |
| 134 | + { |
| 135 | + description: "GetUsername returns local username for ProviderBasicAuth", |
| 136 | + context: &model.UserContext{ |
| 137 | + Provider: model.ProviderBasicAuth, |
| 138 | + Local: &model.LocalContext{BaseContext: model.BaseContext{Username: "bob"}}, |
| 139 | + }, |
| 140 | + run: func(c *model.UserContext) any { return c.GetUsername() }, |
| 141 | + expected: "bob", |
| 142 | + }, |
| 143 | + { |
| 144 | + description: "GetUsername returns LDAP username for ProviderLDAP", |
| 145 | + context: &model.UserContext{ |
| 146 | + Provider: model.ProviderLDAP, |
| 147 | + LDAP: &model.LDAPContext{BaseContext: model.BaseContext{Username: "carol"}}, |
| 148 | + }, |
| 149 | + run: func(c *model.UserContext) any { return c.GetUsername() }, |
| 150 | + expected: "carol", |
| 151 | + }, |
| 152 | + { |
| 153 | + description: "GetUsername returns OAuth username for ProviderOAuth", |
| 154 | + context: &model.UserContext{ |
| 155 | + Provider: model.ProviderOAuth, |
| 156 | + OAuth: &model.OAuthContext{BaseContext: model.BaseContext{Username: "dave"}}, |
| 157 | + }, |
| 158 | + run: func(c *model.UserContext) any { return c.GetUsername() }, |
| 159 | + expected: "dave", |
| 160 | + }, |
| 161 | + { |
| 162 | + description: "GetEmail returns local email for ProviderLocal", |
| 163 | + context: &model.UserContext{ |
| 164 | + Provider: model.ProviderLocal, |
| 165 | + Local: &model.LocalContext{BaseContext: model.BaseContext{Email: "alice@example.com"}}, |
| 166 | + }, |
| 167 | + run: func(c *model.UserContext) any { return c.GetEmail() }, |
| 168 | + expected: "alice@example.com", |
| 169 | + }, |
| 170 | + { |
| 171 | + description: "GetEmail returns local email for ProviderBasicAuth", |
| 172 | + context: &model.UserContext{ |
| 173 | + Provider: model.ProviderBasicAuth, |
| 174 | + Local: &model.LocalContext{BaseContext: model.BaseContext{Email: "bob@example.com"}}, |
| 175 | + }, |
| 176 | + run: func(c *model.UserContext) any { return c.GetEmail() }, |
| 177 | + expected: "bob@example.com", |
| 178 | + }, |
| 179 | + { |
| 180 | + description: "GetEmail returns LDAP email for ProviderLDAP", |
| 181 | + context: &model.UserContext{ |
| 182 | + Provider: model.ProviderLDAP, |
| 183 | + LDAP: &model.LDAPContext{BaseContext: model.BaseContext{Email: "carol@example.com"}}, |
| 184 | + }, |
| 185 | + run: func(c *model.UserContext) any { return c.GetEmail() }, |
| 186 | + expected: "carol@example.com", |
| 187 | + }, |
| 188 | + { |
| 189 | + description: "GetEmail returns OAuth email for ProviderOAuth", |
| 190 | + context: &model.UserContext{ |
| 191 | + Provider: model.ProviderOAuth, |
| 192 | + OAuth: &model.OAuthContext{BaseContext: model.BaseContext{Email: "dave@example.com"}}, |
| 193 | + }, |
| 194 | + run: func(c *model.UserContext) any { return c.GetEmail() }, |
| 195 | + expected: "dave@example.com", |
| 196 | + }, |
| 197 | + { |
| 198 | + description: "GetName returns local name for ProviderLocal", |
| 199 | + context: &model.UserContext{ |
| 200 | + Provider: model.ProviderLocal, |
| 201 | + Local: &model.LocalContext{BaseContext: model.BaseContext{Name: "Alice"}}, |
| 202 | + }, |
| 203 | + run: func(c *model.UserContext) any { return c.GetName() }, |
| 204 | + expected: "Alice", |
| 205 | + }, |
| 206 | + { |
| 207 | + description: "GetName returns local name for ProviderBasicAuth", |
| 208 | + context: &model.UserContext{ |
| 209 | + Provider: model.ProviderBasicAuth, |
| 210 | + Local: &model.LocalContext{BaseContext: model.BaseContext{Name: "Bob"}}, |
| 211 | + }, |
| 212 | + run: func(c *model.UserContext) any { return c.GetName() }, |
| 213 | + expected: "Bob", |
| 214 | + }, |
| 215 | + { |
| 216 | + description: "GetName returns LDAP name for ProviderLDAP", |
| 217 | + context: &model.UserContext{ |
| 218 | + Provider: model.ProviderLDAP, |
| 219 | + LDAP: &model.LDAPContext{BaseContext: model.BaseContext{Name: "Carol"}}, |
| 220 | + }, |
| 221 | + run: func(c *model.UserContext) any { return c.GetName() }, |
| 222 | + expected: "Carol", |
| 223 | + }, |
| 224 | + { |
| 225 | + description: "GetName returns OAuth name for ProviderOAuth", |
| 226 | + context: &model.UserContext{ |
| 227 | + Provider: model.ProviderOAuth, |
| 228 | + OAuth: &model.OAuthContext{BaseContext: model.BaseContext{Name: "Dave"}}, |
| 229 | + }, |
| 230 | + run: func(c *model.UserContext) any { return c.GetName() }, |
| 231 | + expected: "Dave", |
| 232 | + }, |
| 233 | + { |
| 234 | + description: "ProviderName returns 'local' for ProviderLocal", |
| 235 | + context: &model.UserContext{Provider: model.ProviderLocal}, |
| 236 | + run: func(c *model.UserContext) any { return c.ProviderName() }, |
| 237 | + expected: "local", |
| 238 | + }, |
| 239 | + { |
| 240 | + description: "ProviderName returns 'local' for ProviderBasicAuth", |
| 241 | + context: &model.UserContext{Provider: model.ProviderBasicAuth}, |
| 242 | + run: func(c *model.UserContext) any { return c.ProviderName() }, |
| 243 | + expected: "local", |
| 244 | + }, |
| 245 | + { |
| 246 | + description: "ProviderName returns 'ldap' for ProviderLDAP", |
| 247 | + context: &model.UserContext{Provider: model.ProviderLDAP}, |
| 248 | + run: func(c *model.UserContext) any { return c.ProviderName() }, |
| 249 | + expected: "ldap", |
| 250 | + }, |
| 251 | + { |
| 252 | + description: "ProviderName returns OAuth DisplayName for ProviderOAuth", |
| 253 | + context: &model.UserContext{ |
| 254 | + Provider: model.ProviderOAuth, |
| 255 | + OAuth: &model.OAuthContext{DisplayName: "GitHub"}, |
| 256 | + }, |
| 257 | + run: func(c *model.UserContext) any { return c.ProviderName() }, |
| 258 | + expected: "GitHub", |
| 259 | + }, |
| 260 | + { |
| 261 | + description: "TOTPPending returns true for ProviderLocal when TOTPPending is true", |
| 262 | + context: &model.UserContext{ |
| 263 | + Provider: model.ProviderLocal, |
| 264 | + Local: &model.LocalContext{TOTPPending: true}, |
| 265 | + }, |
| 266 | + run: func(c *model.UserContext) any { return c.TOTPPending() }, |
| 267 | + expected: true, |
| 268 | + }, |
| 269 | + { |
| 270 | + description: "TOTPPending returns false for ProviderLocal when TOTPPending is false", |
| 271 | + context: &model.UserContext{ |
| 272 | + Provider: model.ProviderLocal, |
| 273 | + Local: &model.LocalContext{TOTPPending: false}, |
| 274 | + }, |
| 275 | + run: func(c *model.UserContext) any { return c.TOTPPending() }, |
| 276 | + expected: false, |
| 277 | + }, |
| 278 | + { |
| 279 | + description: "TOTPPending returns false for ProviderOAuth", |
| 280 | + context: &model.UserContext{ |
| 281 | + Provider: model.ProviderOAuth, |
| 282 | + OAuth: &model.OAuthContext{}, |
| 283 | + }, |
| 284 | + run: func(c *model.UserContext) any { return c.TOTPPending() }, |
| 285 | + expected: false, |
| 286 | + }, |
| 287 | + { |
| 288 | + description: "TOTPPending returns false for ProviderLDAP", |
| 289 | + context: &model.UserContext{ |
| 290 | + Provider: model.ProviderLDAP, |
| 291 | + LDAP: &model.LDAPContext{}, |
| 292 | + }, |
| 293 | + run: func(c *model.UserContext) any { return c.TOTPPending() }, |
| 294 | + expected: false, |
| 295 | + }, |
| 296 | + { |
| 297 | + description: "OAuthName returns DisplayName for ProviderOAuth", |
| 298 | + context: &model.UserContext{ |
| 299 | + Provider: model.ProviderOAuth, |
| 300 | + OAuth: &model.OAuthContext{DisplayName: "Google"}, |
| 301 | + }, |
| 302 | + run: func(c *model.UserContext) any { return c.OAuthName() }, |
| 303 | + expected: "Google", |
| 304 | + }, |
| 305 | + { |
| 306 | + description: "OAuthName returns empty string for ProviderLocal", |
| 307 | + context: &model.UserContext{ |
| 308 | + Provider: model.ProviderLocal, |
| 309 | + Local: &model.LocalContext{}, |
| 310 | + }, |
| 311 | + run: func(c *model.UserContext) any { return c.OAuthName() }, |
| 312 | + expected: "", |
| 313 | + }, |
| 314 | + { |
| 315 | + description: "OAuthName returns empty string for ProviderLDAP", |
| 316 | + context: &model.UserContext{ |
| 317 | + Provider: model.ProviderLDAP, |
| 318 | + LDAP: &model.LDAPContext{}, |
| 319 | + }, |
| 320 | + run: func(c *model.UserContext) any { return c.OAuthName() }, |
| 321 | + expected: "", |
| 322 | + }, |
| 323 | + } |
| 324 | + |
| 325 | + for _, test := range tests { |
| 326 | + t.Run(test.description, func(t *testing.T) { |
| 327 | + assert.Equal(t, test.expected, test.run(test.context)) |
| 328 | + }) |
| 329 | + } |
| 330 | +} |
0 commit comments