mirror of
https://github.com/joohoi/acme-dns.git
synced 2024-12-22 21:33:51 +07:00
DB tests
This commit is contained in:
parent
d30860eeb8
commit
22db9a8209
123
db_test.go
Normal file
123
db_test.go
Normal file
@ -0,0 +1,123 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRegister(t *testing.T) {
|
||||
_ = DB.Init(":memory:")
|
||||
defer DB.DB.Close()
|
||||
|
||||
// Register tests
|
||||
_, err := DB.Register()
|
||||
if err != nil {
|
||||
t.Errorf("Registration failed, got error [%v]", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetByUsername(t *testing.T) {
|
||||
_ = DB.Init(":memory:")
|
||||
defer DB.DB.Close()
|
||||
|
||||
// Create reg to refer to
|
||||
reg, err := DB.Register()
|
||||
if err != nil {
|
||||
t.Errorf("Registration failed, got error [%v]", err)
|
||||
}
|
||||
|
||||
regUser, err := DB.GetByUsername(reg.Username)
|
||||
if err != nil {
|
||||
t.Errorf("Could not get test user, got error [%v]", err)
|
||||
}
|
||||
|
||||
if reg.Username != regUser.Username {
|
||||
t.Errorf("GetByUsername username [%q] did not match the original [%q]", regUser.Username, reg.Username)
|
||||
}
|
||||
|
||||
if reg.Subdomain != regUser.Subdomain {
|
||||
t.Errorf("GetByUsername subdomain [%q] did not match the original [%q]", regUser.Subdomain, reg.Subdomain)
|
||||
}
|
||||
|
||||
// regUser password already is a bcrypt hash
|
||||
if !CorrectPassword(reg.Password, regUser.Password) {
|
||||
t.Errorf("The password [%s] does not match the hash [%s]", reg.Password, regUser.Password)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetByDomain(t *testing.T) {
|
||||
_ = DB.Init(":memory:")
|
||||
defer DB.DB.Close()
|
||||
|
||||
var regDomain = ACMETxt{}
|
||||
|
||||
// Create reg to refer to
|
||||
reg, err := DB.Register()
|
||||
if err != nil {
|
||||
t.Errorf("Registration failed, got error [%v]", err)
|
||||
}
|
||||
|
||||
regDomainSlice, err := DB.GetByDomain(reg.Subdomain)
|
||||
if err != nil {
|
||||
t.Errorf("Could not get test user, got error [%v]", err)
|
||||
}
|
||||
if len(regDomainSlice) == 0 {
|
||||
t.Errorf("No rows returned for GetByDomain [%s]", reg.Subdomain)
|
||||
} else {
|
||||
regDomain = regDomainSlice[0]
|
||||
}
|
||||
|
||||
if reg.Username != regDomain.Username {
|
||||
t.Errorf("GetByUsername username [%q] did not match the original [%q]", regDomain.Username, reg.Username)
|
||||
}
|
||||
|
||||
if reg.Subdomain != regDomain.Subdomain {
|
||||
t.Errorf("GetByUsername subdomain [%q] did not match the original [%q]", regDomain.Subdomain, reg.Subdomain)
|
||||
}
|
||||
|
||||
// regDomain password already is a bcrypt hash
|
||||
if !CorrectPassword(reg.Password, regDomain.Password) {
|
||||
t.Errorf("The password [%s] does not match the hash [%s]", reg.Password, regDomain.Password)
|
||||
}
|
||||
|
||||
// Not found
|
||||
regNotfound, _ := DB.GetByDomain("does-not-exist")
|
||||
if len(regNotfound) > 0 {
|
||||
t.Errorf("No records should be returned.")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUpdate(t *testing.T) {
|
||||
_ = DB.Init(":memory:")
|
||||
defer DB.DB.Close()
|
||||
|
||||
// Create reg to refer to
|
||||
reg, err := DB.Register()
|
||||
if err != nil {
|
||||
t.Errorf("Registration failed, got error [%v]", err)
|
||||
}
|
||||
|
||||
regUser, err := DB.GetByUsername(reg.Username)
|
||||
if err != nil {
|
||||
t.Errorf("Could not get test user, got error [%v]", err)
|
||||
}
|
||||
|
||||
// Set new values (only TXT should be updated) (matches by username and subdomain)
|
||||
|
||||
validTXT := "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
||||
|
||||
regUser.Password = "nevergonnagiveyouup"
|
||||
regUser.Value = validTXT
|
||||
|
||||
err = DB.Update(regUser)
|
||||
if err != nil {
|
||||
t.Errorf("DB Update failed, got error: [%v]", err)
|
||||
}
|
||||
|
||||
updUser, err := DB.GetByUsername(regUser.Username)
|
||||
if err != nil {
|
||||
t.Errorf("GetByUsername threw error [%v]", err)
|
||||
}
|
||||
if updUser.Value != validTXT {
|
||||
t.Errorf("Update failed, fetched value [%s] does not match the update value [%s]", updUser.Value, validTXT)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user