summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorhorus_arch2015-03-20 01:01:30 +0100
committerhorus_arch2015-03-20 01:01:30 +0100
commit039fb0ce71d132a1dfe93b516bc5953dcb61285e (patch)
tree0680bf2d0d42a7c8f4f5dfe0b579277edfc38198
parent77b167ceae8904d827571a0ba7bfa13fac28a40e (diff)
downloadfreemail-039fb0ce71d132a1dfe93b516bc5953dcb61285e.tar.gz
Bundling functionality in server.go and improving code flow.
-rw-r--r--Makefile10
-rw-r--r--a_test.go2
-rw-r--r--db.go1
-rw-r--r--domain.go11
-rw-r--r--domain_test.go14
-rw-r--r--email.go5
-rw-r--r--email_test.go7
-rw-r--r--server.go37
-rw-r--r--server_test.go16
-rw-r--r--struct.go19
-rw-r--r--user.go2
-rw-r--r--utilities.go12
-rw-r--r--utilities_test.go9
13 files changed, 117 insertions, 28 deletions
diff --git a/Makefile b/Makefile
index 60270b1..a03ba1c 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/a_test.go b/a_test.go
index f8f83c5..debcddb 100644
--- a/a_test.go
+++ b/a_test.go
@@ -1,3 +1,5 @@
+/* For go test with go version < 1.4 */
+
package main
import (
diff --git a/db.go b/db.go
index 10b9650..f24070a 100644
--- a/db.go
+++ b/db.go
@@ -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")
diff --git a/domain.go b/domain.go
index b897bbb..c7b5102 100644
--- a/domain.go
+++ b/domain.go
@@ -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.")
+ }
+}
diff --git a/email.go b/email.go
index 1e9c4e3..90799ee 100644
--- a/email.go
+++ b/email.go
@@ -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.")
+ }
+}
diff --git a/struct.go b/struct.go
index 27b73c1..b8ccf38 100644
--- a/struct.go
+++ b/struct.go
@@ -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
diff --git a/user.go b/user.go
index 885ec84..f491eea 100644
--- a/user.go
+++ b/user.go
@@ -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")
+ }
+}