1/*2Maddy Mail Server - Composable all-in-one email server.3Copyright © 2019-2020 Max Mazurov <fox.cpp@disroot.org>, Maddy Mail Server contributors45This program is free software: you can redistribute it and/or modify6it under the terms of the GNU General Public License as published by7the Free Software Foundation, either version 3 of the License, or8(at your option) any later version.910This program is distributed in the hope that it will be useful,11but WITHOUT ANY WARRANTY; without even the implied warranty of12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the13GNU General Public License for more details.1415You should have received a copy of the GNU General Public License16along with this program. If not, see <https://www.gnu.org/licenses/>.17*/1819package auth2021import (22 "fmt"23 "testing"24)2526func TestCheckDomainAuth(t *testing.T) {27 cases := []struct {28 rawUsername string2930 perDomain bool31 allowedDomains []string3233 loginName string34 }{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 }7677 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 }8687 if loginName != case_.loginName {88 t.Errorf("Incorrect login name, got %s, wanted %s", loginName, case_.loginName)89 }90 })91 }92}