示例#1
0
func TestDelChannelApi(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		// prepare
		d := NewTvChannelDriver(TEST_APP_KEY, ts.Context, wcg.NewLogger(nil))
		ent1 := &tv.TvChannel{"c1", "s1", "foo", "bar"}
		ent2 := &tv.TvChannel{"c2", "s2", "hoge", "piyo"}
		d.Put(d.NewKey(ent1.Key(), 0, nil), ent1)
		d.Put(d.NewKey(ent2.Key(), 0, nil), ent2)

		err := util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 2
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm TvChannel entities has been stored within a timeout window.")
		p := app.Api.Path("/channels/c1/s1.json")
		req := ts.Delete(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)

		err = util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "DELETE %s Confirm TvChannel entities has been deleted via API within a timeout window.", p)

		// Confirm cache invalidation
		mc := memcache.NewDriver(ts.Context, wcg.NewLogger(nil))
		assert.Ok(!mc.Exists(MC_KEY_CHANNELS), "DELETE %s should invalidate the cache", p)

	})
}
示例#2
0
func TestAddKeywordApi(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		p := app.Api.Path("/keywords/")
		req := ts.PostForm(p, url.Values{
			"keyword":  []string{"モーニング娘。'15"},
			"category": []string{"モーニング娘。"},
			"scope":    []string{"1"},
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		var got map[string]interface{}
		res := req.RouteTo(app.Routes())
		res.Json(&got)
		assert.HttpStatus(201, res)
		assert.EqStr(
			"http://localhost:8080/api/pt/keywords/モーニング娘。'15.json",
			got["location"].(string),
			"POST %s location",
			p,
		)

		// Confirm cache invalidation
		mc := memcache.NewDriver(ts.Context, wcg.NewLogger(nil))
		assert.Ok(!mc.Exists(MC_KEY_KEYWORDS), "POST %s should invalidate the cache", p)

	})
}
示例#3
0
func TestSave(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.App, ts)

		// TODO: fixture
		// prepare
		p := &blog.Post{
			Title:     "test post",
			Content:   "test content",
			Tags:      []string{},
			IsDraft:   false,
			IsHidden:  false,
			PublishAt: time.Now(),
		}
		d := NewPostDriver(appCtx)
		_, err := d.Save(p)
		assert.Nil(err, "Save() should not return an error on creating a new post")
		assert.Ok(len(p.Id) > 0, "post Id should be set after Save().")

		lastUpdate := p.UpdatedAt

		_, err = d.Save(p)
		assert.Nil(err, "Save() should not return an error on updating an exising post")
		assert.Ok(p.UpdatedAt.After(lastUpdate), "UpdatedAt should be updated afeter Save()")
	})
}
示例#4
0
func TestApiUpdateEvent(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)
		test.DatastoreFixture(ts.Context, "./fixtures/TestApiUpdateEvent.json", nil)
		d := NewEventDriver(appCtx)

		var e event.Event

		p := app.TestApp().Api.Path("/events/event1.json")
		req := ts.PutForm(p, url.Values{
			"title":      []string{"Updated Title"},
			"link":       []string{"http://www.up.example.com/"},
			"image_link": []string{"http://www.up.example.com/img.png"},
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.TestApp().Routes())
		assert.HttpStatus(200, res)

		err := util.WaitFor(func() bool {
			d.Load("event1", &e)
			return e.Title == "Updated Title"
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm the show has been inserted within timeout window.")

		assert.EqStr("Updated Title", e.Title, "Title")
		assert.EqStr("http://www.up.example.com/", e.Link, "Link")
		assert.EqStr("http://www.up.example.com/img.png", e.ImageLink, "ImageLink")
	})
}
示例#5
0
func TestAddChannelApi(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		p := app.Api.Path("/channels/")
		req := ts.PostForm(p, url.Values{
			"cid":             []string{"c1"},
			"sid":             []string{"s1"},
			"name":            []string{"foo"},
			"iepg_station_id": []string{"bar"},
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		var got map[string]interface{}
		res := req.RouteTo(app.Routes())
		res.Json(&got)
		assert.HttpStatus(201, res)
		assert.EqStr(
			"http://localhost:8080/api/pt/channels/c1/s1.json",
			got["location"].(string),
			"POST %s location",
			p,
		)

		// Confirm cache invalidation
		mc := memcache.NewDriver(ts.Context, wcg.NewLogger(nil))
		assert.Ok(!mc.Exists(MC_KEY_CHANNELS), "POST %s should invalidate the cache", p)

	})
}
示例#6
0
func TestApiCreateEvent_201(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)
		d := NewEventDriver(appCtx)

		var respJson map[string]interface{}
		var e event.Event

		p := app.TestApp().Api.Path("/events/")
		req := ts.PostForm(p, url.Values{
			"title":      []string{"test tour title"},
			"link":       []string{"http://www.example.com/"},
			"image_link": []string{"http://www.example.com/img.png"},
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.TestApp().Routes())
		assert.HttpStatus(201, res)

		err := util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm the show has been inserted within timeout window.")

		res.Json(&respJson)
		d.Load(respJson["id"].(string), &e)
		assert.EqStr("test tour title", e.Title, "Title")
		assert.EqStr("http://www.example.com/", e.Link, "Link")
		assert.EqStr("http://www.example.com/img.png", e.ImageLink, "ImageLink")
	})
}
示例#7
0
func TestApiCreateEvent_400(t *testing.T) {
	var genParams = func(omitName string) url.Values {
		params := url.Values{
			"title":      []string{"test tour title"},
			"link":       []string{"http://www.example.com/"},
			"image_link": []string{"http://www.example.com/img.png"},
		}
		params.Del(omitName)
		return params
	}

	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		test.DatastoreFixture(ts.Context, "./fixtures/TestApiCreateEventShow.json", nil)
		p := app.TestApp().Api.Path("/events/")
		var testOmitParams = []string{
			"title", "link", "image_link",
		}
		for _, paramName := range testOmitParams {
			req := ts.PostForm(p, genParams(paramName))
			lib.SetApiTokenForTest(req, lib.Admin)
			res := req.RouteTo(app.TestApp().Routes())
			assert.HttpStatus(400, res)
		}
	})
}
示例#8
0
func TestRedirect(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		GAEServer.Routes().Get("/l/", Redirect)
		res := ts.Get("/l/").RouteTo(GAEServer.Routes())
		assert.HttpStatus(404, res)

		res = ts.Get("/l/?u=foobar").RouteTo(GAEServer.Routes())
		assert.HttpStatus(404, res)

		res = ts.
			Get(fmt.Sprintf("/l/?u=%s", url.QueryEscape("http://foobar.example.com/"))).
			RouteTo(GAEServer.Routes())
		assert.HttpStatus(302, res)
		assert.HttpHeader("http://foobar.example.com/", res, "Location")

		res = ts.
			Get(fmt.Sprintf("/l/?u=%s", url.QueryEscape("http://foobar.example.com/?foobar"))).
			RouteTo(GAEServer.Routes())
		assert.HttpStatus(302, res)
		assert.HttpHeader("http://foobar.example.com/?foobar", res, "Location")

		res = ts.
			Get(fmt.Sprintf("/l/?u=%s", url.QueryEscape("http://foobar.example.com/?var=val&hoge=fuga"))).
			RouteTo(GAEServer.Routes())
		assert.HttpStatus(302, res)
		assert.HttpHeader("http://foobar.example.com/?var=val&hoge=fuga", res, "Location")

		res = ts.
			Get(fmt.Sprintf("/l/?u=%s", url.QueryEscape("http://foobar.example.com/?var=val&hoge=fuga#hashhoge"))).
			RouteTo(GAEServer.Routes())
		assert.HttpStatus(302, res)
		assert.HttpHeader("http://foobar.example.com/?var=val&hoge=fuga#hashhoge", res, "Location")
	})
}
示例#9
0
func TestDeleteAmebloContents(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)

		// prepare
		amUrl := "http://ameblo.jp/eriko--imai/entry-11980960390.html"
		d := NewAmebloEntryDriver(appCtx)

		t1 := time.Now()
		ent := &ameblo.AmebloEntry{
			Title:      "切り替え〜る",
			Owner:      "今井絵理子",
			Url:        amUrl,
			UpdatedAt:  t1,
			CrawledAt:  time.Time{},
			Content:    "",
			AmLikes:    5,
			AmComments: 10,
		}
		err := updateIndexes(appCtx, []*ameblo.AmebloEntry{ent})
		assert.Nil(err, "UpdateIndexes() should not return an error on creating an ameblo entry")

		p := app.TestApp().Api.Path("/ameblo/contents/今井絵理子.json")
		req := ts.Delete(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.TestApp().Routes())
		assert.HttpStatus(200, res)

		// confirm the content updated.
		var ent1 ameblo.AmebloEntry
		err = d.Get(d.NewKey(amUrl, 0, nil), &ent1)
		assert.Ok(ent1.CrawledAt.Equal(time.Time{}), "DELETE %s should delete the cralwing timestamp.", p)
	})
}
示例#10
0
func TestApiCreateEventShow_400(t *testing.T) {
	var genParams = func(omitName string) url.Values {
		params := url.Values{
			"open_at":    []string{"2015-01-02T06:00:00Z"},
			"start_at":   []string{"2015-01-02T07:00:00Z"},
			"latitude":   []string{"37.39"},
			"longitude":  []string{"140.38"},
			"venue_id":   []string{"153237058036933"},
			"venue_name": []string{"郡山市民文化センター"},
			"pia_link":   []string{"http://pia.jp/link"},
			"ya_keyword": []string{"郡山"},
		}
		params.Del(omitName)
		return params
	}

	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		test.DatastoreFixture(ts.Context, "./fixtures/TestApiCreateEventShow.json", nil)
		p := app.TestApp().Api.Path("/events/event1/shows")
		var testOmitParams = []string{
			"open_at", "start_at", "latitude", "longitude", "venue_id", "venue_name", // "pia_link", "ya_keyword",
		}
		for _, paramName := range testOmitParams {
			req := ts.PostForm(p, genParams(paramName))
			lib.SetApiTokenForTest(req, lib.Admin)
			res := req.RouteTo(app.TestApp().Routes())
			assert.HttpStatus(400, res)
		}
	})
}
示例#11
0
func TestUpdateApiToken(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app, ts)

		// prepare
		d := lib.NewApiTokenDriver(appCtx)
		tok, _ := d.Issue("token1")
		d.Issue("token2")
		util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 2
		}, util.DefaultWaitForTimeout)

		var tokUrl = fmt.Sprintf("/api/admin/api_tokens/%s/", tok.Key())
		// Case 400
		req := ts.PutForm(tokUrl, url.Values{
			"desc":     []string{"updated"},
			"alert_on": []string{"-20"},
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(400, res)

		// Case 200
		req = ts.PutForm(tokUrl, url.Values{
			"desc":     []string{"updated"},
			"alert_on": []string{"30"},
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res = req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)

	})
}
示例#12
0
func TestCreateAndDeleteEvent(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)
		d := NewEventDriver(appCtx)
		t, err := d.Save(&event.Event{
			Title:     "Test Tour",
			Link:      "http://example.com",
			ImageLink: "http://example.com/foo.jpg",
		})
		assert.Nil(err, "TourDriver#Save should not return an error.")
		assert.Ok(t.Id != "", "TourDriver#Save should set Id for the given tour object.")

		err = util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm the tour has been inserted within timeout window.")

		err = d.Delete(t.Id)
		assert.Nil(err, "TourDriver#Delete should not return an error.")

		err = util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm the tour has been deleted within timeout window.")
	})
}
示例#13
0
func Test_updateIndexes(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)
		d := NewAmebloEntryDriver(appCtx)
		amUrl := "http://ameblo.jp/foo/bar.html"
		key := d.NewKey(amUrl, 0, nil)

		t1 := time.Now()
		ent := &ameblo.AmebloEntry{
			Title:      "Test Title",
			Owner:      "Myblog",
			Url:        amUrl,
			UpdatedAt:  t1,
			CrawledAt:  t1,
			Content:    "crawled content",
			AmLikes:    5,
			AmComments: 10,
		}
		err := updateIndexes(appCtx, []*ameblo.AmebloEntry{ent})
		assert.Nil(err, "UpdateIndexes() should not return an error on creating an ameblo entry")

		err = util.WaitFor(func() bool {
			var ent ameblo.AmebloEntry
			d.Get(key, &ent)
			return ent.Url == amUrl
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm entry has been inserted within timeout window.")

		// test confirm not to update content and crawled at
		t2 := time.Now()
		newent := &ameblo.AmebloEntry{
			Title:      "(Updated) Test Title",
			Owner:      "(Updated) Myblog",
			Url:        "http://ameblo.jp/foo/bar.html",
			UpdatedAt:  t2,
			CrawledAt:  t2,
			Content:    "",
			AmLikes:    10,
			AmComments: 15,
		}
		err = updateIndexes(appCtx, []*ameblo.AmebloEntry{newent})
		assert.Nil(err, "UpdateIndexes() should not return an error on creating an ameblo entry")

		err = util.WaitFor(func() bool {
			var updatedent ameblo.AmebloEntry
			d.Get(key, &updatedent)
			return updatedent.AmLikes == 10 && updatedent.AmComments == 15
		}, util.DefaultWaitForTimeout)

		var updatedent ameblo.AmebloEntry
		d.Get(key, &updatedent)
		assert.Nil(err, "AmLikes and AmComments should be updated after UpdateIndexes() without timeout")
		assert.EqStr("crawled content", updatedent.Content, "Content should not be updated after UpdateIndexes()")
		assert.Ok(t1.Unix() == updatedent.CrawledAt.Unix(), "CrawledAt should not be updated UpdateIndexes")
	})
}
示例#14
0
func TestUpdate(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		app := NewApp("default", "TestApp")
		d := NewApiTokenDriver(NewAppContextFromTestServer(app, ts))
		token, err := d.Issue("New Api Token")
		assert.Nil(err, "ApiTokenDriver#Issue should not return an error")
		err = d.Update(token.Token, "Updated Api Token", 3*time.Second)
		assert.Nil(err, "ApiTokenDriver#Update should not return an error")
	})
}
示例#15
0
func TestGetVersion(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		var got interface{}
		assert := test.NewAssert(t)
		req := ts.Get("/api/admin/version/")
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.Routes())
		res.Json(&got)
		assert.HttpStatus(200, res)
	})
}
示例#16
0
func TestApiCreateEventShow_404(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		test.DatastoreFixture(ts.Context, "./fixtures/TestApiCreateEventShow.json", nil)
		p := app.TestApp().Api.Path("/events/notexit/shows")
		req := ts.PostForm(p, url.Values{})
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.TestApp().Routes())
		assert.HttpStatus(404, res)
	})
}
示例#17
0
func TestIssue(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		app := NewApp("default", "TestApp")
		d := NewApiTokenDriver(NewAppContextFromTestServer(app, ts))
		token, err := d.Issue("New Api Token")
		assert.Nil(err, "ApiTokenDriver#Issue should not return an error")
		assert.NotNil(token, "ApiTokenDriver#Issue should issue the token wit")
		assert.EqStr("New Api Token", token.Description, "ApiTokenDriver#Issue should issue the token with given description")
		assert.GtInt(0, len(token.Token), "ApiTokenDriver#Issue should issue the valid token")
	})
}
示例#18
0
func TestGetPost(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.App, ts)

		// TODO: fixture
		// prepare
		d := NewPostDriver(appCtx)
		post1, _ := d.Save(&blog.Post{
			Title:     "test post1",
			Content:   "test content",
			Tags:      []string{},
			IsDraft:   false,
			IsHidden:  false,
			PublishAt: time.Now(),
		})
		post2, _ := d.Save(&blog.Post{
			Title:     "test post2",
			Content:   "test content",
			Tags:      []string{},
			IsDraft:   true,
			IsHidden:  false,
			PublishAt: time.Now(),
		})

		util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)

		// test post1 : public post
		var got *blog.Post
		p := app.Api.Path(fmt.Sprintf("/posts/%s.json", post1.Id))
		req := ts.Get(p)
		res := req.RouteTo(app.Routes())
		res.Json(&got)
		assert.HttpStatus(200, res)
		assert.EqStr("test post1", got.Title, "post1.Title")

		// test post2 : draft post, only visible by admins.
		p = app.Api.Path(fmt.Sprintf("/posts/%s.json", post2.Id))
		req = ts.Get(p)
		res = req.RouteTo(app.Routes())
		assert.HttpStatus(404, res)

		req = ts.Get(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res = req.RouteTo(app.Routes())
		res.Json(&got)
		assert.HttpStatus(200, res)
		assert.EqStr("test post2", got.Title, "post2.Title")
	})
}
示例#19
0
func TestApp(t *testing.T) {
	assert := test.NewAssert(t)
	test.RunTestServer(func(ts *test.TestServer) {
		app := NewApp("default", "TestApp")
		app.Routes().Get("/", func(res *wcg.Response, req *wcg.Request) {
			res.WriteString("OK")
		})
		req := ts.Get("/")
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)
	})
}
示例#20
0
func TestUpdatePost(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.App, ts)

		// TODO: fixture
		// prepare
		d := NewPostDriver(appCtx)
		post1, _ := d.Save(&blog.Post{
			Title:     "test post1",
			Content:   "test content",
			Tags:      []string{},
			IsDraft:   false,
			IsHidden:  false,
			PublishAt: time.Now(),
		})

		util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)

		// test
		p := app.Api.Path(fmt.Sprintf("/posts/%s.json", post1.Id))
		req := ts.PutForm(p, url.Values{
			"title":      []string{"updated: test post1"},
			"content":    []string{"updated: test content"},
			"publish_at": []string{"2015-01-01T12:04:05Z"}, // ISO8601 format
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)

		assert.Nil(util.WaitFor(func() bool {
			var p blog.Post
			key := d.NewKey(post1.Id, 0, nil)
			d.Get(key, &p)
			return "updated: test post1" == p.Title
		}, util.DefaultWaitForTimeout), "Confirm update")

		// case 404 not found
		p = app.Api.Path(fmt.Sprintf("/api/default/posts/%s.json", "not-found"))
		req = ts.PutForm(p, url.Values{
			"title":      []string{"updated: test post1"},
			"content":    []string{"updated: test content"},
			"publish_at": []string{"2015-01-01T12:04:05Z"}, // ISO8601 format
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res = req.RouteTo(app.Routes())
		assert.HttpStatus(404, res)
	})
}
示例#21
0
func TestDeleteRecordApi(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		d := NewRecordCacheDriver(TEST_APP_KEY, ts.Context, wcg.NewLogger(nil))

		// create an empty cache
		var got []tv.TvRecord
		p := app.Api.Path("/records/")
		req := ts.Get(p)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)
		res.Json(&got)
		assert.EqInt(0, len(got), "GET %s should return 2 TvRecord entities.")
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeTvRecord)), "GET %s should create RecordCacheTypeTvRecord cache", p)
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeIEpg)), "GET %s should create RecordCacheTypeIEpg cache", p)

		// prepare
		in_window := genTestRecord()
		in_window.StartAt = d.today.Add(1 * time.Hour)
		in_window.EndAt = d.today.Add(2 * time.Hour)
		d.TvRecord.Save(in_window)

		future := genTestRecord()
		future.StartAt = d.today.Add(RECORD_TIME_WINDOW + 1*time.Hour)
		future.EndAt = future.StartAt.Add(1 * time.Hour)
		d.TvRecord.Save(future)

		err := util.WaitFor(func() bool {
			records, _ := d.TvRecord.NewQuery().Count()
			return records == 2
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm TvRecord entities has been stored within a timeout window.")

		// case 1: future
		p = fmt.Sprintf("%s%s.json", app.Api.Path("/records/"), future.Key())
		req = ts.Delete(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res = req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeTvRecord)), "POST %s should not invalidate RecordCacheTypeTvRecord cache (future record)", p)
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeIEpg)), "GET %s should not invalidate RecordCacheTypeIEpg cache (future record)", p)

		// case 2: in recording window
		p = fmt.Sprintf("%s%s.json", app.Api.Path("/records/"), in_window.Key())
		req = ts.Delete(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res = req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)
		assert.Ok(!d.Cache.Exists(d.GetMcKey(RecordCacheTypeTvRecord)), "POST %s should invalidate RecordCacheTypeTvRecord cache (in_window record)", p)
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeIEpg)), "GET %s should not invalidate RecordCacheTypeIEpg cache (in_window record)", p)
	})
}
示例#22
0
func TestAhAuthHandler(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		app := NewApp("default", "TestApp")

		app.Routes().Get("/_ah/foo", func(res *wcg.Response, req *wcg.Request) {
			assert.EqStr("api:x-appengine-ah", req.User.Id(), "User Id")
			res.WriteString("OK")
		})
		req := ts.Get("/_ah/foo")
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)
	})
}
示例#23
0
func TestAmebloIndexApi(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		var got map[string][]string
		p := app.TestApp().Api.Path("/ameblo/indexes/")
		req := ts.Get(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.TestApp().Routes())
		res.Json(&got)
		assert.HttpStatus(200, res)
		assert.EqInt(2, len(got), "GET %s should return the list of crawled results.", p)
		assert.EqInt(20, len(got["今井絵理子"]), "GET %s return 20 entries for '今井絵理子'.", p)
	})
}
示例#24
0
func TestRevoke(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		app := NewApp("default", "TestApp")
		d := NewApiTokenDriver(NewAppContextFromTestServer(app, ts))
		token, err := d.Issue("New Api Token")
		assert.Nil(err, "ApiTokenDriver#Issue should not return an error")
		err = d.Revoke(token.Token)
		assert.Nil(err, "ApiTokenDriver#Revoke should not return an error")

		err = d.Revoke("non-existing")
		assert.Nil(err, "ApiTokenDriver#Revoke should not return an error even non-existing token is passed")
	})
}
示例#25
0
func TestCreateRecordApi(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		d := NewRecordCacheDriver(TEST_APP_KEY, ts.Context, wcg.NewLogger(nil))

		// create an empty cache
		var got []tv.TvRecord
		p := app.Api.Path("/records/")
		req := ts.Get(p)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)
		res.Json(&got)
		assert.EqInt(0, len(got), "GET %s should return 2 TvRecord entities.")
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeTvRecord)), "GET %s should create RecordCacheTypeTvRecord cache", p)
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeIEpg)), "GET %s should create RecordCacheTypeIEpg cache", p)

		now := time.Now()

		// case 1: future
		req = ts.PostForm(p, url.Values{
			"title":    []string{"Title"},
			"category": []string{"Category"},
			"cid":      []string{"27"},
			"sid":      []string{"hd"},
			"start_at": []string{util.FormatDateTime(now.Add(RECORD_TIME_WINDOW + 24*time.Hour))},
			"end_at":   []string{util.FormatDateTime(now.Add(RECORD_TIME_WINDOW + 25*time.Hour))},
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res = req.RouteTo(app.Routes())
		assert.HttpStatus(201, res)
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeTvRecord)), "POST %s should not invalidate RecordCacheTypeTvRecord cache (future record)", p)
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeIEpg)), "GET %s should not invalidate RecordCacheTypeIEpg cache (future record)", p)

		// case 2: in recording window
		req = ts.PostForm(p, url.Values{
			"title":    []string{"Title"},
			"category": []string{"Category"},
			"cid":      []string{"27"},
			"sid":      []string{"hd"},
			"start_at": []string{util.FormatDateTime(now)},
			"end_at":   []string{util.FormatDateTime(now.Add(1 * time.Hour))},
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res = req.RouteTo(app.Routes())
		assert.HttpStatus(201, res)
		assert.Ok(!d.Cache.Exists(d.GetMcKey(RecordCacheTypeTvRecord)), "POST %s should invalidate RecordCacheTypeTvRecord cache (in window)", p)
		assert.Ok(d.Cache.Exists(d.GetMcKey(RecordCacheTypeIEpg)), "GET %s should not invalidate RecordCacheTypeIEpg cache (in window)", p)
	})
}
示例#26
0
文件: ah_test.go 项目: speedland/apps
func TestAhHandler(t *testing.T) {
	assert := test.NewAssert(t)
	test.RunTestServer(func(ts *test.TestServer) {
		app := NewApp("test", "TestApp")
		var passed = false
		app.Warmup = func(res *wcg.Response, req *wcg.Request) {
			passed = true
		}
		req := ts.Get("/_ah/warmup")
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)
		assert.HttpBodyString("OK", res)
		assert.Ok(passed, "passed")
	})
}
示例#27
0
func TestApiListEventShow(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		test.DatastoreFixture(ts.Context, "./fixtures/TestApiListEventShow.json", nil)
		var respJson []event.Show
		p := app.TestApp().Api.Path("/events/event1/shows")
		req := ts.Get(p)
		res := req.RouteTo(app.TestApp().Routes())
		assert.HttpStatus(200, res)
		res.Json(&respJson)
		assert.EqInt(1, len(respJson), "shows should return 1 element")
		assert.EqStr("event1-show1", respJson[0].Id, "Id")
		assert.EqStr("event1", respJson[0].EventId, "EventId")
	})
}
示例#28
0
func Test_registerJsonpProxyHandlers(t *testing.T) {
	routes := GAEServer.Routes()
	registerJsonpProxyHandlers(routes)
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		res := ts.Get("/jsonp").RouteTo(routes)
		assert.HttpStatus(400, res)

		res = ts.Get("/jsonp?target=ftp://www.example.com").RouteTo(routes)
		assert.HttpStatus(400, res)

		res = ts.Get("/jsonp?target=http://localhost/jsonp").RouteTo(routes)
		assert.HttpStatus(400, res)
	})
}
示例#29
0
func TestAmebloIndexAllApi(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		var got []string
		p := app.TestApp().Api.Path("/ameblo/indexes/今井絵理子.json")
		req := ts.Get(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.TestApp().Routes())
		res.Json(&got)
		assert.HttpStatus(200, res)
		assert.Ok(
			len(got) > 20,
			"GET %s should return more thant 20 entries for '今井絵理子' but got %d.",
			p, len(got))
	})
}
示例#30
0
func TestApiUser(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		app := NewApp("default", "TestApp")
		d := NewApiTokenDriver(NewAppContextFromTestServer(app, ts))
		token, err := d.Issue("New Api Token")
		util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)

		user, err := d.ApiUser(token.Token)
		assert.Nil(err, "ApiTokenDriver#ApiUser should not return an error")
		assert.EqStr(token.Token, user.(*ApiUser).token.Token, "ApiUser should return the valid user that has the token itself")
		assert.GtInt(int(token.LastAccess.Unix()), int(user.(*ApiUser).token.LastAccess.Unix()), "ApiUser should update LastAccess time on the token.")
	})
}