Modified returned JSON structure

This commit is contained in:
Joona Hoikkala
2016-12-03 10:31:15 +02:00
parent 8b3d3f809f
commit cb050c2c92
2 changed files with 9 additions and 10 deletions

8
api.go
View File

@ -58,17 +58,17 @@ func (a authMiddleware) Serve(ctx *iris.Context) {
func webRegisterPost(ctx *iris.Context) {
var regJSON iris.Map
var regStatus int
cslice := cidrslice{}
_ = ctx.ReadJSON(&cslice)
aTXT := ACMETxt{}
_ = ctx.ReadJSON(&aTXT)
// Create new user
nu, err := DB.Register(cslice)
nu, err := DB.Register(aTXT.AllowFrom)
if err != nil {
errstr := fmt.Sprintf("%v", err)
regJSON = iris.Map{"error": errstr}
regStatus = iris.StatusInternalServerError
log.WithFields(log.Fields{"error": err.Error()}).Debug("Error in registration")
} else {
regJSON = iris.Map{"username": nu.Username, "password": nu.Password, "fulldomain": nu.Subdomain + "." + DNSConf.General.Domain, "subdomain": nu.Subdomain, "allowfrom": nu.AllowFrom.JSON()}
regJSON = iris.Map{"username": nu.Username, "password": nu.Password, "fulldomain": nu.Subdomain + "." + DNSConf.General.Domain, "subdomain": nu.Subdomain, "allowfrom": nu.AllowFrom.ValidEntries()}
regStatus = iris.StatusCreated
log.WithFields(log.Fields{"user": nu.Username.String()}).Debug("Created new user")

View File

@ -50,10 +50,10 @@ func TestApiRegister(t *testing.T) {
ContainsKey("password").
NotContainsKey("error")
allowfrom := []interface{}{
"123.123.123.123/32",
"1010.10.10.10/24",
"invalid",
allowfrom := map[string][]interface{}{
"allowfrom": []interface{}{"123.123.123.123/32",
"1010.10.10.10/24",
"invalid"},
}
response := e.POST("/register").
@ -68,8 +68,7 @@ func TestApiRegister(t *testing.T) {
ContainsKey("allowfrom").
NotContainsKey("error")
response.Value("allowfrom").String().Equal("[\"123.123.123.123/32\"]")
response.Value("allowfrom").Array().Elements("123.123.123.123/32")
}
func TestApiRegisterWithMockDB(t *testing.T) {