示例#1
0
func Test_API_Task_List(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_Task_List.json", nil), "fixture")

	now, _ := wcg.ParseDateTime("2016-01-10T00:00:00Z")

	lib.TemporarySetNow(now, func() {
		req := ts.GET("/api/admin/tasks/")
		res := req.RouteTo(instance.Routes())
		assert.HTTPStatus(200, res)

		var got entities.Pagination
		assert.JSONResponse(&got, res)
		assert.EqInt(0, got.Length())
	})

	now, _ = wcg.ParseDateTime("2016-01-09T00:00:00Z")

	lib.TemporarySetNow(now, func() {
		req := ts.GET("/api/admin/tasks/")
		res := req.RouteTo(instance.Routes())
		assert.HTTPStatus(200, res)

		var got entities.Pagination
		assert.JSONResponse(&got, res)
		assert.EqInt(1, got.Length())
	})
}
示例#2
0
func Test_API_Tasks_Crawlers_Ameblo_Posts(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_Tasks_Crawlers_Ameblo_Posts.json", nil, "hplink"), "fixture")

	p := entities.AmebloPost.Query().MustExecute(ts.GET("/").Request)
	postList := p.Data.([]hplink.AmebloPost)
	assert.EqInt(1, len(postList))

	httptest.StartMockServer(func(mock *httptest.MockServer) {
		mock.Routes().GET("/morningmusume-9ki/post.html", middleware.ServeFile("./fixtures/mocks/ameblo.jp//morningmusume-9ki/post.html"))
		crawler.MockExternalResource(map[string]string{
			"http://ameblo.jp/morningmusume-9ki/entry-12201946654.html": fmt.Sprintf("%s//morningmusume-9ki/post.html", mock.BaseURL()),
		}, func() {
			runner := testhelper.NewAsyncTaskTestRunner(t, instance.Routes(), ts)
			runner.OnMonitor(func(testreq *httptest.TestRequest, f func()) {
				testhelper.TemporaryAllow(testreq, "hplink.admin", f)
			})
			runner.OnTrigger(func(testreq *httptest.TestRequest, f func()) {
				testhelper.TemporaryAllow(testreq, "hplink.admin", f)
			})
			runner.Run("/api/hplink/tasks/crawlers/ameblo/posts/", url.Values{})

			p := entities.AmebloPost.Query().MustExecute(ts.GET("/").Request)
			postList := p.Data.([]hplink.AmebloPost)
			assert.EqInt(1, len(postList))
			assert.EqStr("ノーメイク☆譜久村聖", postList[0].Title)
			assert.EqStr("譜久村聖", postList[0].Theme)
			assert.EqStr("morningmusume.mizuki_fukumura", postList[0].MemberKey)

			// It also creates a URL cache
			assert.EqInt(1, entities.URLCache.Query().MustCount(ts.GET("/").Request))
		})
	})
}
示例#3
0
func Test_API_Config_Get_Default(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_Config_Get.json", nil), "fixture")
	testUser := httptest.NewTestUser().SetID("facebook:6789")
	dummy := ts.GET("/")

	configs.TemporarySetConfig(dummy.Request,
		"facebook_page_id", "12345",
		func() {
			messenger.TemporaryOptIn(dummy.Request,
				"12345", testUser.ID(),
				func() {
					var notif pt.MessengerNotification
					req := ts.GET("/api/intern/pt/configs/facebook%3A6789/messenger.json")
					req.Request.User = testUser
					helper.TemporaryAllow(req, "family", func() {
						res := req.RouteTo(instance.Routes())
						assert.HTTPStatus(200, res)
						assert.JSONResponse(&notif, res)
						assert.EqStr("facebook:6789", notif.UserID, "MessengerNotification.UserID")
						assert.OK(!notif.Summary, "MessengerNotification.Summary")
						assert.OK(!notif.OnStart, "MessengerNotification.OnStart")
						assert.OK(!notif.OnEnd, "MessengerNotification.OnEnd")
					})
				},
			)
		},
	)
}
示例#4
0
func Test_API_Tasks_Crawlers_Ameblo_EntryLists(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_Tasks_Crawlers_Ameblo_EntryLists.json", nil, "hplink"), "fixture")

	httptest.StartMockServer(func(mock *httptest.MockServer) {
		mock.Routes().GET("/morningmusume-9ki/entrylist.html", middleware.ServeFile("./fixtures/mocks/ameblo.jp//morningmusume-9ki/entrylist.html"))
		crawler.MockExternalResource(map[string]string{
			"http://ameblo.jp/morningmusume-9ki/entrylist.html": fmt.Sprintf("%s//morningmusume-9ki/entrylist.html", mock.BaseURL()),
		}, func() {
			runner := testhelper.NewAsyncTaskTestRunner(t, instance.Routes(), ts)
			runner.OnMonitor(func(testreq *httptest.TestRequest, f func()) {
				testhelper.TemporaryAllow(testreq, "hplink.admin", f)
			})
			runner.OnTrigger(func(testreq *httptest.TestRequest, f func()) {
				testhelper.TemporaryAllow(testreq, "hplink.admin", f)
			})
			runner.Run("/api/hplink/tasks/crawlers/ameblo/entrylists/", url.Values{})

			p := entities.AmebloPost.Query().Order("-PostAt").MustExecute(ts.GET("/").Request)
			postList := p.Data.([]hplink.AmebloPost)
			assert.EqInt(20, len(postList))
			assert.EqStr("ノーメイク☆譜久村聖", postList[0].Title)
			assert.EqStr("", postList[0].Theme)
			assert.EqStr("morningmusume.mizuki_fukumura", postList[0].MemberKey)
			assert.EqStr("http://ameblo.jp/morningmusume-9ki/", postList[0].SettingsURL)

			p = entities.CrawlerSettings.Query().Filter("URL=", "http://ameblo.jp/morningmusume-9ki/").MustExecute(ts.GET("/").Request)
			assert.EqInt(1, p.Length())
			settings := p.Head().(*hplink.CrawlerSettings)
			assert.EqInt(int(hplink.CrawlerStatusSuccess), int(settings.Status))
		})
	})
}
func Test_API_IEPGRecord_OptInOut_Excluded(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_IEPGRecord_OptInOut.json", nil, "intern.pt"), "fixture")

	req := ts.PUTForm(fmt.Sprintf("/api/intern/pt/iepg/records/%s/opt-in/", "excluded"), url.Values{
		"opt_in": []string{"1"},
	})
	helper.TemporaryAllow(req, "family", func() {
		var got pt.IEPG
		res := req.RouteTo(instance.Routes())
		assert.HTTPStatus(200, res)
		assert.JSONResponse(&got, res)
		assert.OK(got.OptIn, "OptIn")
		assert.OK(!got.OptOut, "OptOut")
	})

	req = ts.PUTForm(fmt.Sprintf("/api/intern/pt/iepg/records/%s/opt-in/", "excluded"), url.Values{
		"opt_in": []string{"0"},
	})
	helper.TemporaryAllow(req, "family", func() {
		var got pt.IEPG
		res := req.RouteTo(instance.Routes())
		assert.HTTPStatus(200, res)
		assert.JSONResponse(&got, res)
		assert.OK(!got.OptIn, "OptIn")
		assert.OK(!got.OptOut, "OptOut")
	})
}
示例#6
0
func Test_API_SystemStats_Query(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_SystemStats_Query.json", nil, "intern.home"), "fixture")

	app := NewApp()
	req := ts.GET("/api/intern/home/stats/servers/dummy/system/")
	res := req.RouteTo(app.Routes())
	assert.HTTPStatus(200, res)
	var got []home.SystemStats
	assert.JSONResponse(&got, res)
	assert.EqInt(0, len(got), string(res.Body))

	req = ts.GET("/api/intern/home/stats/servers/dummy/system/?until=2014-12-01T11:00:00Z")
	res = req.RouteTo(app.Routes())
	assert.HTTPStatus(200, res)
	assert.JSONResponse(&got, res)
	assert.EqInt(2, len(got), string(res.Body))

	tempnow, _ := wcg.ParseDateTime("2014-12-01T11:00:00Z")
	lib.TemporarySetNow(tempnow, func() {
		req := ts.GET("/api/intern/home/stats/servers/dummy/system/")
		res := req.RouteTo(app.Routes())
		assert.HTTPStatus(200, res)
		var got []home.SystemStats
		assert.JSONResponse(&got, res)
		assert.EqInt(2, len(got), string(res.Body))
	})

}
示例#7
0
func Test_ByHeader(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_HeaderAuth.json", nil), "fixture")

	router := wcg.NewRouter()
	handler := wcg.AnonymousHandler(func(res *wcg.Response, req *wcg.Request) {
		res.WriteString(req.User.ID())
	})
	byHeader(router, func() {
		router.GET("/api/foo", handler)
		router.GET("/cron/foo", handler)
	})
	req := ts.GET("/api/foo")
	assert.EqStr("-", string(req.RouteTo(router).Body), "Guest")

	req = ts.GET("/api/foo")
	req.AddHeader(request.APITokenHeader, "2e489f08-3be9-4d28-af86-b14841b2181a")
	assert.EqStr("api:2e489f08-3be9-4d28-af86-b14841b2181a", string(req.RouteTo(router).Body), "APITokenUser")

	req = ts.GET("/api/foo")
	req.AddHeader(request.APITokenHeader, "invalidtoken")
	assert.EqStr("-", string(req.RouteTo(router).Body), "Guest")

	req = ts.GET("/api/foo")
	req.AddHeader(request.TaskQueueHeader, "true")
	assert.EqStr(fmt.Sprintf("api:%s", request.APITokenForTask), string(req.RouteTo(router).Body), "Guest")

	req = ts.GET("/cron/foo")
	req.AddHeader(request.CronHeader, "true")
	assert.EqStr(fmt.Sprintf("api:%s", request.APITokenForCron), string(req.RouteTo(router).Body), "CronUser")

}
示例#8
0
func Test_API_CronStatsCleanup(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_SystemStats_Query.json", nil, "intern.home"), "fixture")

	sk, _ := Server.Get().Key("dummy").MustOne(ts.GET("/").Request)
	c := SystemStats.Query().Ancestor(sk).MustCount(ts.GET("/").Request)
	assert.EqInt(2, c)

	tempnow, _ := wcg.ParseDateTime("2014-12-01T11:00:00Z")
	lib.TemporarySetNow(tempnow, func() {
		app := NewApp()
		req := ts.GET("/cron/intern/home/stats/cleanup/")
		res := req.RouteTo(app.Routes())
		assert.HTTPStatus(200, res)

		// confirm nothing deleted
		c = SystemStats.Query().Ancestor(sk).MustCount(ts.GET("/").Request)
		assert.EqInt(2, c)
	})

	app := NewApp()
	req := ts.GET("/cron/intern/home/stats/cleanup/")
	res := req.RouteTo(app.Routes())
	assert.HTTPStatus(200, res)

	// everything deleted since it's old
	c = SystemStats.Query().Ancestor(sk).MustCount(ts.GET("/").Request)
	assert.EqInt(0, c)
}
示例#9
0
func Test_API_Artist_Member_Get(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_Artist_Member_Get.json", nil, "hplink"), "fixture")
	req := ts.GET("/api/hplink/c-ute/members/maimi_yajima.json")
	res := req.RouteTo(instance.Routes())
	var got map[string]interface{}
	assert.HTTPStatus(200, res)
	assert.JSONResponse(&got, res)
	assert.EqStr("c-ute.maimi_yajima", got["key"].(string))
}
func Test_API_IEPGExclusion_List(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_IEPGExclusion_List.json", nil, "intern.pt"))
	req := ts.GET("/api/intern/pt/iepg/exclusions/")
	res := req.RouteTo(instance.Routes())
	assert.HTTPStatus(200, res)

	var got []pt.IEPGExclusion
	assert.JSONResponse(&got, res)
	assert.EqInt(2, len(got), string(res.Body))
}
示例#11
0
func Test_API_Artist_Members_All(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_Artist_Members_All.json", nil, "hplink"), "fixture")
	req := ts.GET("/api/hplink/c-ute/members/")
	res := req.RouteTo(instance.Routes())
	var got []*hplink.Member
	assert.HTTPStatus(200, res)
	assert.JSONResponse(&got, res)
	assert.EqInt(1, len(got))
	assert.EqStr("c-ute.maimi_yajima", got[0].Key)
}
func Test_API_CrawlerSettings_Delete(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_CrawlerSettings_Delete.json", nil, "hplink"), "fixture")
	req := ts.DELETE("/api/hplink/crawlersettings/http%3A%2F%2Fameblo.jp%2Fc-ute-official%2F.json")
	testhelper.TemporaryAllow(req, "hplink.admin", func() {
		res := req.RouteTo(instance.Routes())
		assert.HTTPStatus(200, res)
		_, settings := entities.CrawlerSettings.Get().Key("http://ameblo.jp/c-ute-official/").MustOne(ts.GET("/").Request)
		assert.Nil(settings)
	})
}
func Test_API_CrawlerSettings_All(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_CrawlerSettings_All.json", nil, "hplink"), "fixture")
	req := ts.GET("/api/hplink/crawlersettings/ameblo/")
	res := req.RouteTo(instance.Routes())
	var got []*hplink.CrawlerSettings
	assert.HTTPStatus(200, res)
	assert.JSONResponse(&got, res)
	assert.EqInt(1, len(got))
	assert.EqStr("http://ameblo.jp/c-ute-official/", got[0].URL)
}
示例#14
0
func Test_API_APIToken_List(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_APIToken_List.json", nil), "fixture")

	req := ts.GET("/api/admin/api_tokens/")
	res := req.RouteTo(instance.Routes())
	assert.HTTPStatus(200, res)

	var got []models.APIToken
	assert.JSONResponse(&got, res)
	assert.EqInt(1, len(got), "ListAPIToken length")
}
示例#15
0
func Test_API_OAuth2ClientSettings_List(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_OAuth2ClientSettings_List.json", nil), "fixture")

	req := ts.GET("/api/admin/oauth2/clients/")
	res := req.RouteTo(instance.Routes())
	assert.HTTPStatus(200, res)

	var got []models.OAuth2ClientSettings
	assert.JSONResponse(&got, res)
	assert.EqInt(1, len(got), "OAuth2ClientSettings length")
}
示例#16
0
func Test_API_IEPGRecord_OptInOut_NonAutoCollected(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_IEPGRecord_OptInOut.json", nil, "intern.pt"), "fixture")

	req := ts.PUTForm(fmt.Sprintf("/api/intern/pt/iepg/records/%s/opt-in/", "iepg1"), url.Values{
		"opt_in": []string{"1"},
	})
	helper.TemporaryAllow(req, "family", func() {
		res := req.RouteTo(instance.Routes())
		assert.HTTPStatus(400, res)
	})
}
示例#17
0
func Test_API_OAuth2ClientSettings_Get(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_OAuth2ClientSettings_Get.json", nil), "fixture")

	// 200 Case
	req := ts.GET("/api/admin/oauth2/clients/client1.json")
	res := req.RouteTo(instance.Routes())
	assert.HTTPStatus(200, res)

	var got models.OAuth2ClientSettings
	assert.JSONResponse(&got, res)
	assert.EqStr("secret1", got.ClientSecret, "ClientSecret")
}
示例#18
0
func Test_API_APIToken_Delete(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_APIToken_Delete.json", nil), "fixture")

	// 200 Case
	req := ts.DELETE("/api/admin/api_tokens/tokenstring.json")
	res := req.RouteTo(instance.Routes())
	assert.HTTPStatus(200, res)

	req = ts.GET("/api/admin/api_tokens/tokenstring.json")
	res = req.RouteTo(instance.Routes())
	assert.HTTPStatus(404, res)
}
示例#19
0
func Test_API_APIToken_Get(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_APIToken_Get.json", nil), "fixture")

	// 200 Case
	req := ts.GET("/api/admin/api_tokens/tokenstring.json")
	res := req.RouteTo(instance.Routes())
	assert.HTTPStatus(200, res)

	var got models.APIToken
	assert.JSONResponse(&got, res)
	assert.EqStr("tokenstring", string(got.Token), "APIToken.Token")
}
示例#20
0
func Test_API_IEPGRecord_List(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_IEPGRecord_List.json", nil, "intern.pt"), "fixture")

	lib.TemporarySetNow(now, func() {
		req := ts.GET("/api/intern/pt/iepg/records/")
		res := req.RouteTo(instance.Routes())
		assert.HTTPStatus(200, res)

		var got entities.Pagination
		assert.JSONResponse(&got, res)
		assert.EqInt(1, got.Length(), "%v", got)
	})
}
示例#21
0
func Test_IsUserOptIn(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_IsUserOptIn.json", nil), "fixture")

	router := wcg.NewRouter()
	router.GET("/test_is_user_opt_in", wcg.AnonymousHandler(func(res *wcg.Response, req *wcg.Request) {
		assert.OK(IsUserOptIn(req, "12345", "67890"), "IsUserOptIn should return true.")
		assert.OK(!IsUserOptIn(req, "aaaa", "aaaa"), "IsUserOptIn should return false.")
		res.WriteString("OK")
	}))

	req := ts.GET("/test_is_user_opt_in")
	res := req.RouteTo(router)
	assert.HTTPStatus(200, res)
}
示例#22
0
func Test_API_IEPGRecord_Delete(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_IEPGRecord_Delete.json", nil, "intern.pt"), "fixture")

	req := ts.DELETE(fmt.Sprintf("/api/intern/pt/iepg/records/%s.json", "iepg1"))
	helper.TemporaryAllow(req, "family", func() {
		res := req.RouteTo(instance.Routes())
		assert.HTTPStatus(200, res)
	})

	req = ts.DELETE(fmt.Sprintf("/api/intern/pt/iepg/records/%s.json", "iepg2"))
	helper.TemporaryAllow(req, "family", func() {
		res := req.RouteTo(instance.Routes())
		assert.HTTPStatus(403, res)
	})
}
func Test_API_CrawlerSettings_Update(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_CrawlerSettings_Update.json", nil, "hplink"), "fixture")
	req := ts.PUTForm("/api/hplink/crawlersettings/http%3A%2F%2Fameblo.jp%2Fc-ute-official%2F.json", url.Values{
		"artist_key": []string{"c-ute"},
	})
	testhelper.TemporaryAllow(req, "hplink.admin", func() {
		res := req.RouteTo(instance.Routes())
		var got hplink.CrawlerSettings
		assert.HTTPStatus(200, res)
		assert.JSONResponse(&got, res)
		assert.EqStr("http://ameblo.jp/c-ute-official/", got.URL)
		assert.EqStr("c-ute", got.ArtistKey)
		_, settings := entities.CrawlerSettings.Get().Key("http://ameblo.jp/c-ute-official/").MustOne(ts.GET("/").Request)
		assert.NotNil(settings)
	})
}
func Test_API_IEPGExclusion_RunTask(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_IEPGExclusion_RunTask.json", nil, "intern.pt"))

	now, _ := wcg.ParseDateTime("2016-01-01T00:00:00Z")
	lib.TemporarySetNow(now, func() {
		runner := helper.NewAsyncTaskTestRunner(t, instance.Routes(), ts)
		runner.OnTrigger(func(req *httptest.TestRequest, trigger func()) {
			helper.TemporaryAllow(req, "family", trigger)
		})
		runner.OnMonitor(func(req *httptest.TestRequest, monitor func()) {
			helper.TemporaryAllow(req, "family", monitor)
		})
		runner.Run("/api/intern/pt/iepg/exclusions/task/", url.Values{})

		req := ts.GET("/dummy")
		_, flagged := IEPG.Get().Key("test1").MustOne(req.Request)
		assert.OK(len(flagged.(*pt.IEPG).ExcludedBy) > 0)

	})
}
示例#25
0
func Test_API_Artist_Member_Update(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_Artist_Member_Update.json", nil, "hplink"), "fixture")
	req := ts.PUTForm("/api/hplink/c-ute/members/maimi_yajima.json", url.Values{
		"generation": []string{"1"},
		"color":      []string{"#ff0000"},
	})
	testhelper.TemporaryAllow(req, "hplink.admin", func() {
		res := req.RouteTo(instance.Routes())
		var got hplink.Member
		assert.HTTPStatus(200, res)
		assert.JSONResponse(&got, res)
		assert.EqStr("c-ute.maimi_yajima", got.Key)
		assert.EqInt(1, got.Generation)
		assert.EqStr("#ff0000", got.Color.ToHexString())
		assert.EqTime(time.Time{}, got.Graduationday)
		_, member := entities.Member.Get().Key("c-ute.maimi_yajima").MustOne(ts.GET("/").Request)
		assert.NotNil(member)
		assert.EqStr("#ff0000", member.(*hplink.Member).Color.ToHexString())
	})
}
示例#26
0
func Test_API_IEPGRecord_Put(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_IEPGRecord_Put.json", nil, "intern.pt"), "fixture")

	req := ts.PUTForm(fmt.Sprintf("/api/intern/pt/iepg/records/%s.json", "iepg1"), url.Values{
		"program_title": []string{"Test Program"},
		"start_at":      []string{"2015-01-01T19:00:00.000Z"},
		"end_at":        []string{"2015-01-01T19:00:00.000Z"},
		"category":      []string{"test"},
		"cid":           []string{"21"},
		"sid":           []string{"aa"},
	})
	helper.TemporaryAllow(req, "family", func() {
		res := req.RouteTo(instance.Routes())
		assert.HTTPStatus(200, res)

		var got pt.IEPG
		assert.JSONResponse(&got, res)
		assert.EqStr("Test Program", got.ProgramTitle, "program_title")
		assert.EqStr("21", got.CID, "CID")
		assert.EqStr("aa", got.SID, "SID")
		assert.EqInt(int(pt.IEPGSourceUser), int(got.Source), "Source")
	})

	req = ts.PUTForm(fmt.Sprintf("/api/intern/pt/iepg/records/%s.json", "iepg2"), url.Values{
		"program_title": []string{"Test Program"},
		"start_at":      []string{"2015-01-01T19:00:00.000Z"},
		"end_at":        []string{"2015-01-01T19:00:00.000Z"},
		"category":      []string{"test"},
		"cid":           []string{"21"},
		"sid":           []string{"aa"},
	})
	helper.TemporaryAllow(req, "family", func() {
		res := req.RouteTo(instance.Routes())
		assert.HTTPStatus(403, res)
	})
}
示例#27
0
func Test_API_IEPGKeyword_Crawl_Preview(t *testing.T) {
	assert := gaetest.NewAssert(t)
	assert.Nil(gaetest.ResetMemcache(ts.Context))
	assert.Nil(gaetest.ResetFixtureFromFile(ts.Context, "./fixtures/Test_API_IEPGKeyword_Crawl.json", nil, "intern.pt"), "fixture")

	httptest.StartMockServer(func(mock *httptest.MockServer) {
		// preparation for dummy entry
		mock.Routes().GET("/*", middleware.StaticFile("", "./fixtures/mocks/"))

		// Preview Ttest
		req := ts.POSTForm("/api/intern/pt/iepg/keywords/preview/", url.Values{
			"keyword": []string{"テスト"},
			"scope":   []string{"0"},
		})

		var feedIDs []string
		req.Request.SetLocal("__test_iepg_url_resolver", &iepgURLResolver{
			getFeedURL: func(keyword string, scope pt.IEPGCrawlerScope) string {
				assert.EqStr("テスト", keyword, "keyword")
				assert.EqInt(int(pt.IEPGCrawlerScopeAll), int(scope), "scope")
				return fmt.Sprintf(
					"%s/feed/feed.rss",
					mock.BaseURL(),
				)
			},
			getDataURL: func(id string) string {
				feedIDs = append(feedIDs, id)
				return fmt.Sprintf(
					"%s/data/%s.txt",
					mock.BaseURL(),
					id,
				)
			},
		})

		helper.TemporaryAllow(req, "family", func() {
			res := req.RouteTo(instance.Routes())
			assert.HTTPStatus(200, res)
			assert.EqInt(4, len(feedIDs), "collected Feed IDs")
			assert.EqStr("succ", feedIDs[0], "feedIDs[0]")
			assert.EqStr("opted-out", feedIDs[1], "feedIDs[1]")
			assert.EqStr("excluded", feedIDs[2], "feedIDs[2]")
			assert.EqStr("multiple-categories", feedIDs[3], "feedIDs[3]")
			var got map[string][]pt.IEPG
			assert.JSONResponse(&got, res)
			assert.EqInt(4, len(got["hits"]), "len(got[\"hits\"])")

			var hits = got["hits"]
			var succ = hits[0]
			var optout = hits[1]
			var excluded = hits[2]
			var multiplecategories = hits[3]

			// assertion for succ
			assert.EqStr("DFS00430", succ.StationID, "succ.StationID")
			assert.EqStr("テレビ東京", succ.StationName, "succ.StationName")
			assert.EqStr(
				"The\u3000Girls\u3000Live\u3000▽道重さゆみ卒業ライブに密着▽LoVendoЯスタジオライブ",
				succ.ProgramTitle,
				"succ.ProgramTitle",
			)
			assert.EqInt(2014, succ.StartAt.Year(), "succ.StartAt.Year()")
			assert.EqInt(12, int(succ.StartAt.Month()), "succ.StartAt.Month()")
			assert.EqInt(5, succ.StartAt.Day(), "succ.StartAt.Day()")
			assert.EqInt(23, succ.StartAt.Hour(), "succ.StartAt.Hour()")
			assert.EqInt(0, succ.StartAt.Minute(), "succ.StartAt.Minute()")
			assert.EqInt(2014, succ.EndAt.Year(), "succ.EndAt.Year()")
			assert.EqInt(12, int(succ.EndAt.Month()), "succ.EndAt.Month()")
			assert.EqInt(6, succ.EndAt.Day(), "succ.EndAt.Day()")
			assert.EqInt(1, succ.EndAt.Hour(), "succ.EndAt.Hour()")
			assert.EqInt(30, succ.EndAt.Minute(), "succ.EndAt.Minute()")
			assert.EqStr("23", succ.CID, "succ.CID")
			assert.EqStr("hd", succ.SID, "succ.SID")
			assert.Not(succ.OptIn, "succ.OptIn")
			assert.Not(succ.OptOut, "succ.OptOut")
			assert.EqInt(0, len(succ.ExcludedBy), "len(succ.ExcludedBy)")
			assert.EqInt(0, len(succ.CrawledBy), "len(succ.CrawledBy)")

			// assertion for optout
			assert.Not(optout.OptIn, "OptIn")
			assert.OK(optout.OptOut, "OptOut")
			assert.EqInt(0, len(optout.ExcludedBy), "len(ExcludedBy)")

			// assertion for excluded
			assert.Not(excluded.OptIn, "excluded.OptIn")
			assert.Not(excluded.OptOut, "excluded.OptOut")
			assert.EqInt(1, len(excluded.ExcludedBy), "len(excluded.ExcludedBy)")
			assert.EqStr("crawl-test-exclusion", excluded.ExcludedBy[0], "excluded.ExcludedBy[0]")

			// assertion for multiple categories.
			assert.EqInt(1, len(multiplecategories.CrawledBy), "len(multiplecategories.CrawledBy)")
			assert.EqStr("another-crawl-keyword", multiplecategories.CrawledBy[0], "multiplecategories.CrawledBy[0]")

		})
	})
}