diff options
| -rw-r--r-- | Makefile | 10 | ||||
| -rw-r--r-- | a_test.go | 2 | ||||
| -rw-r--r-- | db.go | 1 | ||||
| -rw-r--r-- | domain.go | 11 | ||||
| -rw-r--r-- | domain_test.go | 14 | ||||
| -rw-r--r-- | email.go | 5 | ||||
| -rw-r--r-- | email_test.go | 7 | ||||
| -rw-r--r-- | server.go | 37 | ||||
| -rw-r--r-- | server_test.go | 16 | ||||
| -rw-r--r-- | struct.go | 19 | ||||
| -rw-r--r-- | user.go | 2 | ||||
| -rw-r--r-- | utilities.go | 12 | ||||
| -rw-r--r-- | utilities_test.go | 9 |
13 files changed, 117 insertions, 28 deletions
@@ -1,15 +1,15 @@ # Modify env.sh and uncomment to change config. include env.sh -# Start the race detector with "TEST_RACE=1 make test" on the command line. -ifdef TEST_RACE +# Start the race detector with "RACE=1 make test" on the command line. +ifdef RACE RACE:=-race else RACE:= endif -# To be verbose while testing use TEST_VERBOSE=1. -ifdef TEST_VERBOSE +# To be verbose while testing use VERBOSE=1. +ifdef VERBOSE VERBOSE:=-v else VERBOSE:= @@ -83,7 +83,7 @@ pack: gen_config (echo "Run \"make build\" first." && exit 1) test: import - FREEMAIL_DB_CREDENTIALS=":memory:" go test $(RACE) $(VERBOSE) #-tags general + FREEMAIL_DB_CREDENTIALS=":memory:" go test $(RACE) $(VERBOSE) @(rm $(IMPORT_FILE) 2>/dev/null && echo "Removing import file..." ) || true test_all: import @@ -1,3 +1,5 @@ +/* For go test with go version < 1.4 */ + package main import ( @@ -30,6 +30,7 @@ func InitDB() { Db.Debug().AutoMigrate(&vA) Db.Model(&vU).AddUniqueIndex("idx_virtuser_email", "email") + Db.Model(&vU).AddForeignKey("domain_id", "virtual_domains(id)", "CASCADE", "RESTRICT") /* Db.Model(&u).AddUniqueIndex("idx_user_name", "name") Db.Model(&u).AddUniqueIndex("idx_user_email", "email") @@ -3,6 +3,7 @@ package main import ( "log" "net" + "strings" ) func (vD VirtualDomain) DomainExists() bool { @@ -44,3 +45,13 @@ func (vD VirtualDomain) ValidateDomain() bool { } return true } + +func GetDomain(email string) string { + fragments := strings.Split(email, "@") + return fragments[len(fragments)-1] +} + +func (vD VirtualDomain) GetPrimaryKey() int64 { + Db.Find(&vD) + return vD.Id +} diff --git a/domain_test.go b/domain_test.go index d9dbb0c..6f90ede 100644 --- a/domain_test.go +++ b/domain_test.go @@ -40,3 +40,17 @@ func TestValidateDomain(t *testing.T) { t.Fatal("'" + d.Name + "' is a valid domain.") } } + +func TestGetDomain(t *testing.T) { + domain := GetDomain("foo@example.org") + if domain != "example.org" { + t.Fatal("Can't get the domain from the adress.") + } +} + +func TestGetPrimaryKey(t *testing.T) { + d := VirtualDomain{Name: "example.org"} + if d.GetPrimaryKey() != 1 { + t.Fatal("Primary key should be 1.") + } +} @@ -52,8 +52,3 @@ func ValidateAlias(alias string) bool { } return true } - -func GetDomain(email string) string { - fragments := strings.Split(email, "@") - return fragments[len(fragments)-1] -} diff --git a/email_test.go b/email_test.go index 431d6e3..1f102ee 100644 --- a/email_test.go +++ b/email_test.go @@ -50,10 +50,3 @@ func TestValidateAlias(t *testing.T) { t.Fatal("'foo' is not a valid alias.") } } - -func TestGetDomain(t *testing.T) { - domain := GetDomain("foo@example.org") - if domain != "example.org" { - t.Fatal("Can't get the domain from the adress.") - } -} diff --git a/server.go b/server.go new file mode 100644 index 0000000..a0146b9 --- /dev/null +++ b/server.go @@ -0,0 +1,37 @@ +package main + +import ( + "errors" +) + +func CreateNewEntry(email, password string) error { + if !ValidateEmail(email) { + return errors.New("This doesn't look like a mail adress.") + } + + vU := VirtualUser{Email: email, Password: password} + + if vU.EmailExists() { + return errors.New("Mail adress already exists.") + } + + vD := VirtualDomain{} + vD.Name = GetDomain(vU.Email) + + if !vD.ValidateDomain() { + return errors.New("This doesn't look like a good domain. Host not found.") + } + + if !vD.DomainExists() { + if !vD.CreateDomain() { + return errors.New("There was an error.") + } + } + + vU.DomainId = vD.GetPrimaryKey() + + if !vU.CreateEmail() { + return errors.New("There was an error.") + } + return nil +} diff --git a/server_test.go b/server_test.go new file mode 100644 index 0000000..209e3c4 --- /dev/null +++ b/server_test.go @@ -0,0 +1,16 @@ +package main + +import ( + "testing" +) + +func TestCreateNewEntry(t *testing.T) { + err := CreateNewEntry("foobar@example.org", "password") + if err != nil { + t.Fatal(err) + } + err = CreateNewEntry("foo", "") + if err == nil { + t.Fatal("CreateNewEntry: Allow wrong input, error is nil.") + } +} @@ -17,26 +17,25 @@ type User struct { // Domains for which we do E-Mail hosting type VirtualDomain struct { - Id int64 + Id int64 + Name string + //DomainId int64 `gorm:"primary_key"` // User User - UserId int64 - Name string + //UserId int64 } // User for postfix type VirtualUser struct { - Id int64 - UserId int64 - VirtualDomain VirtualDomain - VirtualDomainId int64 - Password string - Email string + Id int64 + DomainId int64 + Password string + Email string + //UserId int64 } // E-Mail aliase for postfix type VirtualAliase struct { Id int64 - VirtualDomain VirtualDomain VirtualDomainId int64 Source string Destination string @@ -24,7 +24,7 @@ func (vU VirtualUser) UpdatePassword(password string) bool { if vU.Password == password { return false } - query := Db.Model(&vU).Update("password", password) + query := Db.Model(&vU).Where("Email = ?", vU.Email).Update("password", password) if query.Error != nil { log.Println(query.Error) return false diff --git a/utilities.go b/utilities.go index a090ea7..60423dc 100644 --- a/utilities.go +++ b/utilities.go @@ -14,3 +14,15 @@ func Md5Hash(content string) string { return hash } + +// Checks for equality in strings and returns true all are equal +func CompareStrings(strs ...string) bool { + for k, _ := range strs { + if k < len(strs)-1 { + if strs[k] != strs[k+1] { + return false + } + } + } + return true +} diff --git a/utilities_test.go b/utilities_test.go index a8d291b..61d0b27 100644 --- a/utilities_test.go +++ b/utilities_test.go @@ -14,3 +14,12 @@ func TestMd5Hash(t *testing.T) { t.Fatal("Expected %s as hash. Got %s.", hash, testHash) } } + +func TestCompareStrings(t *testing.T) { + if !CompareStrings("foo", "foo", "foo") { + t.Fatal("All strings are equal. Expected true.") + } + if CompareStrings("bar", "foo", "hello world", "523") { + t.Fatal("Strings are not equal. Exptected false") + } +} |
