diff options
| -rw-r--r-- | a_test.go | 9 | ||||
| -rw-r--r-- | domain.go | 46 | ||||
| -rw-r--r-- | domain_test.go | 42 | ||||
| -rw-r--r-- | main_test.go | 15 | ||||
| -rw-r--r-- | user.go | 33 | ||||
| -rw-r--r-- | user_test.go | 46 | ||||
| -rw-r--r-- | utilities.go | 16 | ||||
| -rw-r--r-- | utilities_test.go | 16 |
8 files changed, 223 insertions, 0 deletions
diff --git a/a_test.go b/a_test.go new file mode 100644 index 0000000..f8f83c5 --- /dev/null +++ b/a_test.go @@ -0,0 +1,9 @@ +package main + +import ( + "testing" +) + +func TestMain(t *testing.T) { + InitDB() +} diff --git a/domain.go b/domain.go new file mode 100644 index 0000000..b897bbb --- /dev/null +++ b/domain.go @@ -0,0 +1,46 @@ +package main + +import ( + "log" + "net" +) + +func (vD VirtualDomain) DomainExists() bool { + query := Db.Where("name = ?", vD.Name).Find(&vD) + if query.Error != nil { + log.Println(query.Error) + return false + } + if vD.Name == "" { + return false + } + return true +} + +func (vD VirtualDomain) CreateDomain() bool { + if !Db.Debug().NewRecord(vD) { + log.Println("Creating new record failed.", vD.Name) + return false + } + query := Db.Debug().Create(&vD) + if query.Error != nil { + log.Println(query.Error) + return false + } + return true +} + +func (vD VirtualDomain) ValidateDomain() bool { + if vD.Name == "" { + return false + } + addr, err := net.LookupIP(vD.Name) + if err != nil { + log.Println(err) + return false + } + if len(addr) == 0 { + return false + } + return true +} diff --git a/domain_test.go b/domain_test.go new file mode 100644 index 0000000..d9dbb0c --- /dev/null +++ b/domain_test.go @@ -0,0 +1,42 @@ +package main + +import ( + "testing" +) + +func TestCreateDomain(t *testing.T) { + d := VirtualDomain{Name: "example.org"} + if !d.CreateDomain() { + t.Fatal("Creating domain failed.") + } +} + +func TestDomainExists(t *testing.T) { + d := VirtualDomain{Name: "blablabla"} + if d.DomainExists() { + t.Fatal("'" + d.Name + "' does not exists.") + } + d.Name = "example.org" + if !d.DomainExists() { + t.Fatal("'" + d.Name + "' should exist.") + } +} + +func TestValidateDomain(t *testing.T) { + d := VirtualDomain{Name: "blablabla"} + if d.ValidateDomain() { + t.Fatal(d.Name + " is not a valid domain.") + } + d.Name = "dfalsdf.adfjalf.example.org" + if d.ValidateDomain() { + t.Fatal("'" + d.Name + "' is not a valid domain.") + } + d.Name = "https://google.com" + if d.ValidateDomain() { + t.Fatal("'" + d.Name + "' is not a valid domain.") + } + d.Name = "google.com" + if !d.ValidateDomain() { + t.Fatal("'" + d.Name + "' is a valid domain.") + } +} diff --git a/main_test.go b/main_test.go new file mode 100644 index 0000000..dfb296e --- /dev/null +++ b/main_test.go @@ -0,0 +1,15 @@ +package main + +/* +//Go version 1.4+ only. + +import ( + "os" + "testing" +) + +func TestMain(m *testing.M) { + InitDB() + os.Exit(m.Run()) +} +*/ @@ -0,0 +1,33 @@ +package main + +import ( + "log" +) + +func (vU VirtualUser) HashPassword() string { + return Md5Hash(vU.Password) +} + +func (vU VirtualUser) AuthUser() bool { + passwd := vU.Password + Db.Where("email = ?", vU.Email).Find(&vU) + if vU.Password == passwd { + return true + } + return false +} + +func (vU VirtualUser) UpdatePassword(password string) bool { + if password == "" { + return false + } + if vU.Password == password { + return false + } + query := Db.Model(&vU).Update("password", password) + if query.Error != nil { + log.Println(query.Error) + return false + } + return true +} diff --git a/user_test.go b/user_test.go new file mode 100644 index 0000000..3c2bc74 --- /dev/null +++ b/user_test.go @@ -0,0 +1,46 @@ +package main + +import ( + "testing" +) + +func TestHashPassword(t *testing.T) { + vU := VirtualUser{} + vU.Password = "md5hash" + vU.Password = vU.HashPassword() + if vU.Password != "f9d08276bc85d30d578e8883f3c7e843" { + t.Fatal("Expected 'f9d08276bc85d30d578e8883f3c7e843' as hash. Got %s.", vU.Password) + } +} + +func TestAuthUser(t *testing.T) { + vU := VirtualUser{Email: "test@example.com", Password: "password"} + if !vU.AuthUser() { + t.Fatal("Authentification failed.") + } + vU.Password = "Password" + if vU.AuthUser() { + t.Fatal("Authentification succeeded with wrong password.") + } +} + +func TestUpdatePassword(t *testing.T) { + vU := VirtualUser{Email: "test@example.com", Password: "password"} + if vU.UpdatePassword(vU.Password) { + t.Fatal("Update password even when both passwords be equal.") + } + if vU.UpdatePassword("") { + t.Fatal("Update password even with empty password.") + } + if !vU.UpdatePassword("Password") { + t.Fatal("Updating password failed.") + } + +} + +func TestCreateAlias(t *testing.T) { + //vA := VirtualAlias(Source: "") +} + +func TestAliasExists(t *testing.T) { +} diff --git a/utilities.go b/utilities.go new file mode 100644 index 0000000..a090ea7 --- /dev/null +++ b/utilities.go @@ -0,0 +1,16 @@ +package main + +import ( + "crypto/md5" + "fmt" + "io" +) + +// Hashs and returns a string (md5) +func Md5Hash(content string) string { + h := md5.New() + io.WriteString(h, content) + hash := fmt.Sprintf("%x", h.Sum(nil)) + + return hash +} diff --git a/utilities_test.go b/utilities_test.go new file mode 100644 index 0000000..a8d291b --- /dev/null +++ b/utilities_test.go @@ -0,0 +1,16 @@ +// +build all general + +package main + +import ( + "testing" +) + +func TestMd5Hash(t *testing.T) { + hash := "f9d08276bc85d30d578e8883f3c7e843" + testHash := Md5Hash("md5hash") + + if hash != testHash { + t.Fatal("Expected %s as hash. Got %s.", hash, testHash) + } +} |
