Ejemplo n.º 1
0
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:        481,
			ClientTS:          11,
			ConcurrentOrder:   0,
			ClientName:        "Castcloud",
			ClientDescription: "oink",
		},
	}))
	assert.Equal(t, 200, req.send().Code)

	// There should now be 2 events
	req.Method = "GET"
	res := checkEvents(t, req)
	assert.Len(t, res.Events, 2)
}
Ejemplo n.º 2
0
func TestAddCast(t *testing.T) {
	r := createRouter()
	cast := &Cast{}

	// It should return an existing cast
	req := testRequest(r, "POST", "/library/casts", nil)
	req.Header.Set("Authorization", "token")
	req.PostForm = url.Values{}
	req.PostForm.Set("feedurl", "test.go")
	res := req.send()
	assert.Equal(t, 200, res.Code)
	json.Unmarshal(res.Body.Bytes(), cast)
	assert.Equal(t, "test.go", cast.URL)
	assert.Equal(t, "test", cast.Name)

	// There should still be only 2 subscriptions
	user := store.GetUser("test")
	assert.Len(t, user.Subscriptions, 2)

	// It should return 500 when the crawling fails
	req.PostForm.Set("feedurl", "dat_url")
	res = req.send()
	assert.Equal(t, 500, res.Code)

	// There should still be only 2 subscriptions
	user = store.GetUser("test")
	assert.Len(t, user.Subscriptions, 2)

	// It should return a new cast
	req.PostForm.Set("feedurl", testRSS)
	res = req.send()
	assert.Equal(t, 200, res.Code)
	json.Unmarshal(res.Body.Bytes(), cast)
	assert.Equal(t, testRSS, cast.URL)
	assert.Equal(t, "BSD Now HD", cast.Name)
	assert.NotNil(t, cast.Feed)

	// There should now be 3 subscriptions
	user = store.GetUser("test")
	assert.Len(t, user.Subscriptions, 3)

	// The new cast should be in the store
	cast = store.GetCastByURL(testRSS)
	assert.NotNil(t, cast)
	assert.Equal(t, "BSD Now HD", cast.Name)
	assert.NotNil(t, cast.Feed)
}
Ejemplo n.º 3
0
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 TestEvents(t *testing.T) {
	err := store.AddEvents([]Event{
		Event{
			Type:              10,
			EpisodeID:         1,
			PositionTS:        134,
			ClientTS:          100,
			ConcurrentOrder:   0,
			ClientName:        "Castcloud",
			ClientDescription: "oink",
		},
		Event{
			Type:              10,
			EpisodeID:         1,
			PositionTS:        112,
			ClientTS:          50,
			ConcurrentOrder:   0,
			ClientName:        "Castcloud",
			ClientDescription: "oink",
		},
	}, 2, "evuuid1")
	assert.Nil(t, err)

	events := store.GetEvents(2, 100, "")
	assert.Len(t, events, 0)

	events = store.GetEvents(2, 99, "")
	assert.Len(t, events, 1)
	assert.Equal(t, 10, events[0].Type)
	assert.Equal(t, 134, events[0].PositionTS)

	events = store.GetEvents(2, 99, "evuuid1")
	assert.Len(t, events, 0)

	events = store.GetEvents(2, 99, "nope")
	assert.Len(t, events, 1)

	events = store.GetEvents(2, 25, "")
	assert.Len(t, events, 2)
	assert.Equal(t, 112, events[0].PositionTS)
}
Ejemplo n.º 5
0
func TestGetNewEpisodes(t *testing.T) {
	r := createRouter()

	req := testRequest(r, "GET", "/library/newepisodes?since=32503679999", nil)
	req.Header.Set("Authorization", "token")
	res := checkNewEpisodes(t, req)
	assert.Len(t, res.Episodes, 2)
	assert.Equal(t, Episode{
		ID:     2,
		CastID: 69,
	}, res.Episodes[0])
	assert.Equal(t, Episode{
		ID:     3,
		CastID: 69,
	}, res.Episodes[1])

	req.URL, _ = url.Parse("/library/newepisodes?since=32503680000")
	res = checkNewEpisodes(t, req)
	assert.Len(t, res.Episodes, 1)
	assert.Equal(t, Episode{
		ID:     3,
		CastID: 69,
	}, res.Episodes[0])

	req.URL, _ = url.Parse("/library/newepisodes?since=32503680001")
	res = checkNewEpisodes(t, req)
	assert.Len(t, res.Episodes, 0)

	req.URL, _ = url.Parse("/library/newepisodes")
	res = checkNewEpisodes(t, req)
	assert.True(t, len(res.Episodes) > 2)

	req.URL, _ = url.Parse("/library/newepisodes?since=what")
	resp := req.send()
	assert.Equal(t, 400, resp.Code)
}
Ejemplo n.º 6
0
func TestCreateToken(t *testing.T) {
	token, err := createToken(32)
	assert.Nil(t, err)
	assert.Len(t, token, 64)
}