maddy

Fork https://github.com/foxcpp/maddy

git clone git://git.lin.moe/go/maddy.git

 1/*
 2Maddy Mail Server - Composable all-in-one email server.
 3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors
 4
 5This program is free software: you can redistribute it and/or modify
 6it under the terms of the GNU General Public License as published by
 7the Free Software Foundation, either version 3 of the License, or
 8(at your option) any later version.
 9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program.  If not, see <https://www.gnu.org/licenses/>.
17*/
18
19package auth
20
21import (
22	"fmt"
23	"testing"
24)
25
26func TestCheckDomainAuth(t *testing.T) {
27	cases := []struct {
28		rawUsername string
29
30		perDomain      bool
31		allowedDomains []string
32
33		loginName string
34	}{
35		{
36			rawUsername: "username",
37			loginName:   "username",
38		},
39		{
40			rawUsername:    "username",
41			allowedDomains: []string{"example.org"},
42			loginName:      "username",
43		},
44		{
45			rawUsername:    "username@example.org",
46			allowedDomains: []string{"example.org"},
47			loginName:      "username",
48		},
49		{
50			rawUsername:    "username@example.com",
51			allowedDomains: []string{"example.org"},
52		},
53		{
54			rawUsername:    "username",
55			allowedDomains: []string{"example.org"},
56			perDomain:      true,
57		},
58		{
59			rawUsername:    "username@example.com",
60			allowedDomains: []string{"example.org"},
61			perDomain:      true,
62		},
63		{
64			rawUsername:    "username@EXAMPLE.Org",
65			allowedDomains: []string{"exaMPle.org"},
66			perDomain:      true,
67			loginName:      "username@EXAMPLE.Org",
68		},
69		{
70			rawUsername:    "username@example.org",
71			allowedDomains: []string{"example.org"},
72			perDomain:      true,
73			loginName:      "username@example.org",
74		},
75	}
76
77	for _, case_ := range cases {
78		t.Run(fmt.Sprintf("%+v", case_), func(t *testing.T) {
79			loginName, allowed := CheckDomainAuth(case_.rawUsername, case_.perDomain, case_.allowedDomains)
80			if case_.loginName != "" && !allowed {
81				t.Fatalf("Unexpected authentication fail")
82			}
83			if case_.loginName == "" && allowed {
84				t.Fatalf("Expected authentication fail, got %s as login name", loginName)
85			}
86
87			if loginName != case_.loginName {
88				t.Errorf("Incorrect login name, got %s, wanted %s", loginName, case_.loginName)
89			}
90		})
91	}
92}