|
| 1 | +/* |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package oidchelper |
| 19 | + |
| 20 | +import "testing" |
| 21 | + |
| 22 | +func TestIsUserAllowed(t *testing.T) { |
| 23 | + cases := []struct { |
| 24 | + name string |
| 25 | + cfg Config |
| 26 | + email string |
| 27 | + want bool |
| 28 | + }{ |
| 29 | + { |
| 30 | + name: "no restrictions", |
| 31 | + cfg: Config{}, |
| 32 | + email: "user@example.com", |
| 33 | + want: true, |
| 34 | + }, |
| 35 | + { |
| 36 | + name: "allowed email", |
| 37 | + cfg: Config{ |
| 38 | + AllowEmails: map[string]struct{}{ |
| 39 | + "user@example.com": {}, |
| 40 | + }, |
| 41 | + }, |
| 42 | + email: "user@example.com", |
| 43 | + want: true, |
| 44 | + }, |
| 45 | + { |
| 46 | + name: "blocked email", |
| 47 | + cfg: Config{ |
| 48 | + AllowEmails: map[string]struct{}{ |
| 49 | + "user@example.com": {}, |
| 50 | + }, |
| 51 | + }, |
| 52 | + email: "other@example.com", |
| 53 | + want: false, |
| 54 | + }, |
| 55 | + { |
| 56 | + name: "allowed domain", |
| 57 | + cfg: Config{ |
| 58 | + AllowDomains: map[string]struct{}{ |
| 59 | + "example.com": {}, |
| 60 | + }, |
| 61 | + }, |
| 62 | + email: "user@example.com", |
| 63 | + want: true, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "blocked domain", |
| 67 | + cfg: Config{ |
| 68 | + AllowDomains: map[string]struct{}{ |
| 69 | + "example.com": {}, |
| 70 | + }, |
| 71 | + }, |
| 72 | + email: "user@other.com", |
| 73 | + want: false, |
| 74 | + }, |
| 75 | + { |
| 76 | + name: "email case insensitive", |
| 77 | + cfg: Config{ |
| 78 | + AllowEmails: map[string]struct{}{ |
| 79 | + "user@example.com": {}, |
| 80 | + }, |
| 81 | + }, |
| 82 | + email: "USER@example.com", |
| 83 | + want: true, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: "domain case insensitive", |
| 87 | + cfg: Config{ |
| 88 | + AllowDomains: map[string]struct{}{ |
| 89 | + "example.com": {}, |
| 90 | + }, |
| 91 | + }, |
| 92 | + email: "user@EXAMPLE.COM", |
| 93 | + want: true, |
| 94 | + }, |
| 95 | + { |
| 96 | + name: "invalid email", |
| 97 | + cfg: Config{ |
| 98 | + AllowDomains: map[string]struct{}{ |
| 99 | + "example.com": {}, |
| 100 | + }, |
| 101 | + }, |
| 102 | + email: "not-an-email", |
| 103 | + want: false, |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + for _, tc := range cases { |
| 108 | + t.Run(tc.name, func(t *testing.T) { |
| 109 | + if got := tc.cfg.IsUserAllowed(tc.email); got != tc.want { |
| 110 | + t.Errorf( |
| 111 | + "IsUserAllowed(%q) = %v, want %v", |
| 112 | + tc.email, |
| 113 | + got, |
| 114 | + tc.want, |
| 115 | + ) |
| 116 | + } |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
0 commit comments