Skip to content

Commit

Permalink
Implement settings
Browse files Browse the repository at this point in the history
  • Loading branch information
khlieng committed Jul 18, 2015
1 parent da5012e commit dc6085c
Show file tree
Hide file tree
Showing 9 changed files with 929 additions and 321 deletions.
49 changes: 49 additions & 0 deletions api/account.go
Expand Up @@ -3,6 +3,8 @@ package api
import (
"crypto/rand"
"encoding/hex"
"encoding/json"
"strconv"

"github.com/Castcloud/castcloud-go-server/Godeps/_workspace/src/github.com/labstack/echo"
"github.com/Castcloud/castcloud-go-server/Godeps/_workspace/src/golang.org/x/crypto/bcrypt"
Expand Down Expand Up @@ -63,6 +65,53 @@ func ping(c *echo.Context) error {
return nil
}

//
// GET /account/settings
//
func getSettings(c *echo.Context) error {
user := c.Get("user").(*User)
uuid := c.Get("uuid").(string)
return c.JSON(200, store.GetSettings(user.ID, uuid))
}

//
// POST /account/settings
//
func setSettings(c *echo.Context) error {
data := form(c, "json")
if data == "" {
return c.NoContent(400)
}

settings := []Setting{}
err := json.Unmarshal([]byte(data), &settings)
if err != nil {
return c.NoContent(400)
}

user := c.Get("user").(*User)
uuid := c.Get("uuid").(string)
return store.SaveSettings(settings, user.ID, uuid)
}

//
// DELETE /account/settings/:id
//
func removeSetting(c *echo.Context) error {
id, err := strconv.ParseUint(c.Param("id"), 10, 64)
if err != nil {
return c.NoContent(400)
}

user := c.Get("user").(*User)
err = store.RemoveSetting(id, user.ID)
if err == ErrSettingNotFound {
return c.String(404, "Setting not found")
}

return err
}

func createToken(length int) (string, error) {
b := make([]byte, length)
_, err := rand.Read(b)
Expand Down
136 changes: 136 additions & 0 deletions api/account_test.go
Expand Up @@ -2,9 +2,12 @@ package api

import (
"encoding/json"
"net/url"
"testing"

"github.com/Castcloud/castcloud-go-server/Godeps/_workspace/src/github.com/stretchr/testify/assert"

. "github.com/Castcloud/castcloud-go-server/api/schema"
)

func TestLogin(t *testing.T) {
Expand Down Expand Up @@ -59,6 +62,139 @@ func TestPing(t *testing.T) {
assert.Equal(t, 200, req.send().Code)
}

func TestGetSettings(t *testing.T) {
r := createRouter()

// It should return 200 and a list of settings
req := testRequest(r, "GET", "/account/settings", nil)
req.Header.Set("Authorization", "token")
res := req.send()
assert.Equal(t, 200, res.Code)
assert.Equal(t, testJSON([]Setting{}), res.Body.String())
}

func TestSetSettings(t *testing.T) {
r := createRouter()

// It should return 400 when there is no json in the body
req := testRequest(r, "POST", "/account/settings", nil)
req.Header.Set("Authorization", "token")
assert.Equal(t, 400, req.send().Code)

// It should return 400 if the json is invalid
req.PostForm = url.Values{}
req.PostForm.Set("json", "not_json")
assert.Equal(t, 400, req.send().Code)

// It should return 200 when adding a new setting
req.PostForm.Set("json", testJSON([]Setting{
Setting{
Name: "test",
Value: "a",
},
}))
assert.Equal(t, 200, req.send().Code)

// It should have added the setting
req.Method = "GET"
res := req.send()
assert.Equal(t, 200, res.Code)
settings := []Setting{}
err := json.Unmarshal(res.Body.Bytes(), &settings)
assert.Nil(t, err)
assert.Contains(t, settings, Setting{
ID: 1,
Name: "test",
Value: "a",
})

// It should return 200 when updating a setting
req.Method = "POST"
req.PostForm.Set("json", testJSON([]Setting{
Setting{
Name: "test",
Value: "b",
},
}))
assert.Equal(t, 200, req.send().Code)

// It should have updated the setting
req.Method = "GET"
res = req.send()
assert.Equal(t, 200, res.Code)
settings = []Setting{}
err = json.Unmarshal(res.Body.Bytes(), &settings)
assert.Nil(t, err)
assert.Contains(t, settings, Setting{
ID: 1,
Name: "test",
Value: "b",
})

// It should return 200 when doing a clientspecific setting update
req.Method = "POST"
req.PostForm.Set("json", testJSON([]Setting{
Setting{
Name: "test",
Value: "c",
ClientSpecific: true,
},
}))
assert.Equal(t, 200, req.send().Code)

// It should have updated the setting and should not return the
// old non-specific version of it
req.Method = "GET"
res = req.send()
assert.Equal(t, 200, res.Code)
settings = []Setting{}
err = json.Unmarshal(res.Body.Bytes(), &settings)
assert.Nil(t, err)
assert.Contains(t, settings, Setting{
ID: 2,
Name: "test",
Value: "c",
ClientSpecific: true,
})
assert.NotContains(t, settings, Setting{
ID: 1,
Name: "test",
Value: "b",
})
}

func TestRemoveSetting(t *testing.T) {
r := createRouter()

// It should return 400 if the id is invalid
req := testRequest(r, "DELETE", "/account/settings/cake", nil)
req.Header.Set("Authorization", "token")
assert.Equal(t, 400, req.send().Code)

// It should return 404 when the setting does not exist
req.URL.Path = "/account/settings/1881"
assert.Equal(t, 404, req.send().Code)

// It should return 200 when removing a setting
req.URL.Path = "/account/settings/1"
assert.Equal(t, 200, req.send().Code)

// It should have removed the setting
req.Method = "GET"
req.URL.Path = "/account/settings"
res := req.send()
assert.Equal(t, 200, res.Code)
settings := []Setting{}
err := json.Unmarshal(res.Body.Bytes(), &settings)
assert.Nil(t, err)

for _, setting := range settings {
if setting.ID == 1 {
t.Error("Setting with ID 1 did not get removed")
}
}
}

func TestCreateToken(t *testing.T) {
token, err := createToken(32)
assert.Nil(t, err)
Expand Down
15 changes: 8 additions & 7 deletions api/api.go
Expand Up @@ -85,20 +85,17 @@ func createRouter() *echo.Echo {
account := r.Group("/account")
account.Post("/login", login)
account.Get("/ping", ping)
/*account.Get("/settings")
account.Post("/settings")
account.Delete("/settings/:id")
account.Get("/takeout")*/
account.Get("/settings", getSettings)
account.Post("/settings", setSettings)
account.Delete("/settings/:id", removeSetting)
//account.Get("/takeout")

casts := r.Group("/library/casts")
casts.Get("", getCasts)
casts.Post("", addCast)
casts.Put("/:id", renameCast)
casts.Delete("/:id", removeCast)

// Perhaps:
// /newepisodes -> /episodes?since=0
// /episodes/label/:label -> /episodes?label=label
episodes := r.Group("/library")
episodes.Get("/newepisodes", getNewEpisodes)
episodes.Get("/episodes/:castid", getEpisodes)
Expand All @@ -119,6 +116,10 @@ func createRouter() *echo.Echo {
opml.Get("/casts.opml")
opml.Post("/casts.opml")*/

// Perhaps:
// /newepisodes -> /episodes?since=0
// /episodes/label/:label -> /episodes?label=label

return r
}

Expand Down
8 changes: 8 additions & 0 deletions api/schema/schema.go
Expand Up @@ -20,6 +20,14 @@ type Client struct {
Name string
}

type Setting struct {
ID uint64 `json:"settingid"`
Name string `json:"setting"`
Value string `json:"value"`
ClientSpecific bool `json:"clientspecific"`
ClientUUID string `json:"-"`
}

type Cast struct {
ID uint64 `json:"id"`
URL string `json:"url"`
Expand Down

0 comments on commit dc6085c

Please sign in to comment.