-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
1,001 additions
and
593 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,64 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"strconv" | ||
"time" | ||
|
||
"github.com/Castcloud/castcloud-go-server/Godeps/_workspace/src/github.com/labstack/echo" | ||
|
||
. "github.com/Castcloud/castcloud-go-server/api/schema" | ||
) | ||
|
||
type events struct { | ||
Timestamp int64 `json:"timestamp"` | ||
Events []Event `json:"events"` | ||
} | ||
|
||
// | ||
// GET /library/events | ||
// | ||
func getEvents(c *echo.Context) error { | ||
now := time.Now().Unix() | ||
var err error | ||
var ts uint64 | ||
since := c.Request().URL.Query().Get("since") | ||
if since != "" { | ||
ts, err = strconv.ParseUint(since, 10, 64) | ||
if err != nil { | ||
return c.NoContent(400) | ||
} | ||
} | ||
|
||
user := c.Get("user").(*User) | ||
uuid := "" | ||
excludeSelf := c.Request().URL.Query().Get("exclude_self") | ||
if excludeSelf == "true" { | ||
uuid = c.Get("uuid").(string) | ||
} | ||
|
||
return c.JSON(200, events{ | ||
Timestamp: now, | ||
Events: store.GetEvents(user.ID, ts, uuid), | ||
}) | ||
} | ||
|
||
// | ||
// POST /library/events | ||
// | ||
func addEvents(c *echo.Context) error { | ||
data := form(c, "json") | ||
if data == "" { | ||
return c.NoContent(400) | ||
} | ||
|
||
events := []Event{} | ||
err := json.Unmarshal([]byte(data), &events) | ||
if err != nil { | ||
return c.NoContent(400) | ||
} | ||
|
||
user := c.Get("user").(*User) | ||
uuid := c.Get("uuid").(string) | ||
return store.AddEvents(events, user.ID, uuid) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package api | ||
|
||
import ( | ||
"encoding/json" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/Castcloud/castcloud-go-server/Godeps/_workspace/src/github.com/stretchr/testify/assert" | ||
|
||
. "github.com/Castcloud/castcloud-go-server/api/schema" | ||
) | ||
|
||
func TestGetEvents(t *testing.T) { | ||
r := createRouter() | ||
|
||
// It should return 400 if since is bad | ||
req := testRequest(r, "GET", "/library/events?since=yesterday", nil) | ||
req.Header.Set("Authorization", "evtest1") | ||
assert.Equal(t, 400, req.send().Code) | ||
|
||
// It should return 200 with a timestamp and a list of events | ||
req.URL, _ = url.Parse("/library/events?since=5") | ||
res := checkEvents(t, req) | ||
assert.Len(t, res.Events, 1) | ||
|
||
// It should exclude events sent from this clients UUID when | ||
// exclude_self is true | ||
req.URL, _ = url.Parse("/library/events?since=5&exclude_self=true") | ||
res = checkEvents(t, req) | ||
assert.Len(t, res.Events, 0) | ||
} | ||
|
||
func checkEvents(t *testing.T, req testReq) events { | ||
now := time.Now().Unix() | ||
res := req.send() | ||
assert.Equal(t, 200, res.Code) | ||
data := events{} | ||
err := json.Unmarshal(res.Body.Bytes(), &data) | ||
assert.Nil(t, err) | ||
assert.True(t, data.Timestamp >= now) | ||
return data | ||
} | ||
|
||
func TestAddEvents(t *testing.T) { | ||
r := createRouter() | ||
|
||
// It should return 400 if json is not set | ||
req := testRequest(r, "POST", "/library/events", nil) | ||
req.Header.Set("Authorization", "evtest1") | ||
assert.Equal(t, 400, req.send().Code) | ||
|
||
// It should return 400 if the json is bad | ||
req.PostForm = url.Values{} | ||
req.PostForm.Set("json", "real_bad_json") | ||
assert.Equal(t, 400, req.send().Code) | ||
|
||
// It should return 200 when proper json is sent | ||
req.PostForm.Set("json", testJSON([]Event{ | ||
Event{ | ||
Type: 30, | ||
EpisodeID: 10, | ||
PositionTS: 911, | ||
ClientTS: 10, | ||
ConcurrentOrder: 0, | ||
ClientName: "Castcloud", | ||
ClientDescription: "oink", | ||
}, | ||
})) | ||
assert.Equal(t, 200, req.send().Code) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.