From e14cf986a916e1a7361b058224ef3badd3aad776 Mon Sep 17 00:00:00 2001 From: Horus3 Date: Thu, 19 Mar 2015 16:24:10 +0100 Subject: OOP. Creates virtual_domains and virtual_users now. Enhanced test suite. --- Makefile | 2 +- email.go | 48 +++++++++++++++++++++++++++++++++++++++--------- email_test.go | 56 +++++++++++++++++++++++++++++++++++++++----------------- roadmap.txt | 8 ++++++-- 4 files changed, 85 insertions(+), 29 deletions(-) diff --git a/Makefile b/Makefile index 4e92af6..60270b1 100644 --- a/Makefile +++ b/Makefile @@ -84,7 +84,7 @@ pack: gen_config test: import FREEMAIL_DB_CREDENTIALS=":memory:" go test $(RACE) $(VERBOSE) #-tags general - make clean + @(rm $(IMPORT_FILE) 2>/dev/null && echo "Removing import file..." ) || true test_all: import FREEMAIL_DB_CREDENTIALS=":memory:" go test $(RACE) $(VERBOSE) -v -tags all diff --git a/email.go b/email.go index 150b7fb..1e9c4e3 100644 --- a/email.go +++ b/email.go @@ -2,11 +2,11 @@ package main import ( "log" + "strings" ) -func EmailExists(email string) bool { - vU := VirtualUser{} - query := Db.Where(map[string]interface{}{"email": email}).Find(&vU) +func (vU VirtualUser) EmailExists() bool { + query := Db.Where("email = ?", vU.Email).Find(&vU) if query.Error != nil { log.Println(query.Error) return false @@ -17,13 +17,43 @@ func EmailExists(email string) bool { return true } -func CreateEmail(email, password string) (bool, error) { - vU := VirtualUser{Email: email, Password: password} +func (vU VirtualUser) CreateEmail() bool { if !Db.Debug().NewRecord(vU) { - log.Println("Creating new record failed.") - return false, nil + log.Println("Creating new record failed.", vU) + return false + } + query := Db.Debug().Create(&vU) + if query.Error != nil { + log.Println(query.Error) + return false + } + return true +} + +func ValidateEmail(email string) bool { + if !strings.Contains(email, "@") { + return false + } + if strings.HasPrefix(email, "@") { + return false } - Db.Debug().Create(&vU) + if strings.HasSuffix(email, "@") { + return false + } + if strings.Count(email, "@") != 1 { + return false + } + return true +} + +func ValidateAlias(alias string) bool { + if !strings.Contains(alias, "@") { + return false + } + return true +} - return true, nil +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 887eb8d..431d6e3 100644 --- a/email_test.go +++ b/email_test.go @@ -6,32 +6,54 @@ import ( ) func TestCreateEmail(t *testing.T) { - InitDB() + vU := VirtualUser{Email: "test@example.com", Password: "password"} t.Log("Using database: " + os.Getenv("FREEMAIL_DB_CREDENTIALS")) - if success, err := CreateEmail("test@example.com", "password"); err != nil || !success { - t.Fatal("Can't create email.", err) + if !vU.CreateEmail() { + t.Fatal("Can't create email.") } else { t.Log("test@example.com") } } + func TestEmailExists(t *testing.T) { t.Log("Using database: " + os.Getenv("FREEMAIL_DB_CREDENTIALS")) - vU := VirtualUser{} - vU.Email = "test@example.com" - /* - vU := VirtualUser{} - vU.Email = "test@example.com" - //if err := Db.Save(&vU); err != nil { - if !Db.NewRecord(&vU) { - //t.Fatal("Saving record to database failed.", err) - t.Fatal("Saving record to database failed.") - } - */ - if !EmailExists(vU.Email) { - t.Log(EmailExists(vU.Email)) + vU := VirtualUser{Email: "test@example.com"} + if !vU.EmailExists() { t.Fatal("Email should exist. Expected bool true.") } - if EmailExists("foo@example.com") { + vU.Email = "foo@example.com" + if vU.EmailExists() { t.Fatal("Email doesn't exist. Expected bool false.") } } + +func TestValidateEmail(t *testing.T) { + if ValidateEmail("foo") { + t.Fatal("'foo' is no valid email.") + } + if ValidateEmail("foo@") { + t.Fatal("'foo@' is no valid email.") + } + if ValidateEmail("@foo") { + t.Fatal("'foo' is no valid email.") + } + if ValidateEmail("foo@bar@foo") { + t.Fatal("'foo@bar@foo' is not a valid email.") + } + if !ValidateEmail("foo@bar") { + t.Fatal("'foo@bar' is a valid email.") + } +} + +func TestValidateAlias(t *testing.T) { + if ValidateAlias("foo") { + 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/roadmap.txt b/roadmap.txt index c08644d..6c09e2d 100644 --- a/roadmap.txt +++ b/roadmap.txt @@ -1,5 +1,5 @@ -1.email erstellen -2.passwort ändern +[x] 1.email erstellen +[x] 2.passwort ändern 3.email alias erstellen 4.email löschen @@ -11,3 +11,7 @@ wenn nein -> schreib in datenbank 2) kontrolle passwort -> email && email -> passwort ändere passwort in datenbank + +3) +kontrolle ob alias besteht +wenn nein -> schreib in datenbank -- cgit v1.2.3