Modified returned JSON structure

This commit is contained in:
Joona Hoikkala 2016-12-03 10:31:15 +02:00
parent 8b3d3f809f
commit cb050c2c92
No known key found for this signature in database
GPG Key ID: C14AAE0F5ADCB854
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) { func webRegisterPost(ctx *iris.Context) {
var regJSON iris.Map var regJSON iris.Map
var regStatus int var regStatus int
cslice := cidrslice{} aTXT := ACMETxt{}
_ = ctx.ReadJSON(&cslice) _ = ctx.ReadJSON(&aTXT)
// Create new user // Create new user
nu, err := DB.Register(cslice) nu, err := DB.Register(aTXT.AllowFrom)
if err != nil { if err != nil {
errstr := fmt.Sprintf("%v", err) errstr := fmt.Sprintf("%v", err)
regJSON = iris.Map{"error": errstr} regJSON = iris.Map{"error": errstr}
regStatus = iris.StatusInternalServerError regStatus = iris.StatusInternalServerError
log.WithFields(log.Fields{"error": err.Error()}).Debug("Error in registration") log.WithFields(log.Fields{"error": err.Error()}).Debug("Error in registration")
} else { } 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 regStatus = iris.StatusCreated
log.WithFields(log.Fields{"user": nu.Username.String()}).Debug("Created new user") 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"). ContainsKey("password").
NotContainsKey("error") NotContainsKey("error")
allowfrom := []interface{}{ allowfrom := map[string][]interface{}{
"123.123.123.123/32", "allowfrom": []interface{}{"123.123.123.123/32",
"1010.10.10.10/24", "1010.10.10.10/24",
"invalid", "invalid"},
} }
response := e.POST("/register"). response := e.POST("/register").
@ -68,8 +68,7 @@ func TestApiRegister(t *testing.T) {
ContainsKey("allowfrom"). ContainsKey("allowfrom").
NotContainsKey("error") 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) { func TestApiRegisterWithMockDB(t *testing.T) {