Esempio n. 1
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)
	})
}
Esempio n. 2
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()")
	})
}
Esempio n. 3
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")
	})
}
Esempio n. 4
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)

	})
}
Esempio n. 5
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.")
	})
}
Esempio n. 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")
	})
}
Esempio n. 7
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")
	})
}
Esempio n. 8
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")
	})
}
Esempio n. 9
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)
	})
}
Esempio n. 10
0
func TestNewEventShowList(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := wcg.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)
		test.DatastoreFixture(ts.Context, "./fixtures/TestNewEventShowList.json", nil)

		var showList []event.Show
		d := NewShowDriver(appCtx)
		d.NewQuery().GetAll(&showList)
		assert.EqInt(2, len(showList), "2 Show entities must be prepared.")
		list, _ := NewEventShowList(appCtx, showList)

		// showList[1] is orphan Show entity so that only 1 entity is returned.
		assert.EqInt(1, len(list), "1 eventShow must be returned.")
	})
}
Esempio n. 11
0
func TestAmebloCrawlContents(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)
		refd := NewAmebloRefDriver(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")
		err = util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm AmebloEntry has been indexed.")

		p := app.TestApp().Api.Path("/ameblo/contents/")
		req := ts.Get(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(len(ent1.Content) > 0, "GET %s should update the content", p)

		// confirm the reference updated
		c, _ := refd.NewQuery().Count()
		assert.EqInt(1, c, "GET %s should update the content reference", p)
	})
}
Esempio n. 12
0
func TestApiCreateEventShow(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/TestApiCreateEventShow.json", nil)
		d := NewShowDriver(appCtx)

		var respJson map[string]interface{}
		var show event.Show
		p := app.TestApp().Api.Path("/events/event1/shows")
		req := ts.PostForm(p, 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{"郡山"},
		})
		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), "event1", &show)
		open_at, _ := util.ParseDateTime("2015-01-02T06:00:00Z")
		start_at, _ := util.ParseDateTime("2015-01-02T07:00:00Z")
		assert.EqTime(open_at, show.OpenAt, "OpenAt")
		assert.EqTime(start_at, show.StartAt, "StartAt")
		assert.EqFloat64(37.39, show.Latitude, "Latitude")
		assert.EqFloat64(140.38, show.Longitude, "Longitude")
		assert.EqStr("153237058036933", show.VenueId, "VenueId")
		assert.EqStr("郡山市民文化センター", show.VenueName, "VenueName")
		assert.EqStr("http://pia.jp/link", show.PiaLink, "PiaLink")
		assert.EqStr("郡山", show.YAKeyword, "YAKeyword")
	})
}
Esempio n. 13
0
func TestIssueApiToken(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app, ts)
		req := ts.PostForm("/api/admin/api_tokens/", url.Values{
			"desc": []string{"new token"},
		})
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(201, res)

		var got []*models.ApiToken
		d := lib.NewApiTokenDriver(appCtx)
		util.WaitFor(func() bool {
			d.NewQuery().GetAll(&got)
			return got != nil && len(got) == 1
		}, util.DefaultWaitForTimeout)
		assert.EqStr("new token", got[0].Description, "IssueApiToken Description")
	})
}
Esempio n. 14
0
func TestApiDeleteEventShow(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/TestApiDeleteEventShow.json", nil)
		d := NewShowDriver(appCtx)

		p := app.TestApp().Api.Path("/events/event1/shows/event1-show1.json")
		req := ts.Delete(p)
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.TestApp().Routes())
		assert.HttpStatus(200, res)

		err := util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 0
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Confirm the show has been inserted within timeout window.")
	})
}
Esempio n. 15
0
func TestAmebloHistoryInsights(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)

		// prepare
		d := NewAmebloEntryDriver(appCtx)
		amUrl := "http://ameblo.jp/eriko--imai/entry-11980960390.html"
		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")

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

		var got amebloHistoryInsights
		p := app.TestApp().Api.Path("/ameblo/insights/今井絵理子/history.json")
		req := ts.Get(p)
		res := req.RouteTo(app.TestApp().Routes())
		assert.HttpStatus(200, res)
		res.Json(&got)
		assert.EqInt(5, got.TotalLikes, "GET %s TotalLikes", p)
		assert.EqInt(10, got.TotalComments, "GET %s TotalComments", p)
		assert.EqInt(1, got.TotalPosts, "GET %s TotalPosts", p)
		assert.EqInt(1, len(got.History), "GET %s length of history", p)
		assert.EqStr(amUrl, got.History[0].Url, "GET %s hitory Url", p)
	})
}
Esempio n. 16
0
func Test_generateYACrawlerKeywords(t *testing.T) {
	// set base time to skip
	now = func() time.Time {
		return time.Date(2015, 01, 01, 15, 0, 0, 0, time.UTC)
	}

	test.RunTestServer(func(ts *test.TestServer) {
		assert := wcg.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)
		test.DatastoreFixture(ts.Context, "./fixtures/Test_generateYACrawlerKeywords.json", nil)

		keywords, err := generateYACrawlerKeywords(appCtx)
		assert.Nil(err, "generateYACCrawlerKeywords should not return an error")
		assert.EqInt(2, len(keywords), "generateYACCrawlerKeywords should return 1 keyword object.")

		// no updates should happen
		keywords, err = generateYACrawlerKeywords(appCtx)
		assert.Nil(err, "generateYACCrawlerKeywords should not return an error")
		assert.EqInt(0, len(keywords), "generateYACCrawlerKeywords should return 1 keyword object.")
	})
}
Esempio n. 17
0
func TestDeletePost(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
		req := ts.Delete(fmt.Sprintf("/api/default/posts/%s.json", post1.Id))
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)

		assert.Nil(util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 0
		}, util.DefaultWaitForTimeout), "Confirm deletion")

		// case 200 not found even the post does not exist.
		req = ts.Delete(fmt.Sprintf("/api/default/posts/%s.json", "not-found"))
		lib.SetApiTokenForTest(req, lib.Admin)
		res = req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)
	})
}
Esempio n. 18
0
func TestCronApiTokens(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app, ts)

		// prepare
		d := lib.NewApiTokenDriver(appCtx)
		tok1, _ := d.Issue("token1")
		tok2, _ := d.Issue("token2")
		d.Issue("token3") // not monitored
		util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 3
		}, util.DefaultWaitForTimeout)

		now := time.Now()
		tok1.LastAccess = now
		tok1.AlertOn = 1 * time.Hour
		key := d.NewKey(tok1.Key(), 0, nil)
		d.SyncPut(key, tok1)

		tok2.LastAccess = now.Add(-1 * 60 * 60 * time.Second) // 1 hour before
		tok2.AlertOn = 30 * time.Second
		key = d.NewKey(tok2.Key(), 0, nil)
		d.SyncPut(key, tok2)

		// test
		var got map[string]int
		req := ts.Get("/cron/admin/api_tokens/")
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.Routes())
		res.Json(&got)

		assert.EqInt(1, got["alerted_tokens"], "alerted_tokens")
		assert.EqInt(2, got["monitored_tokens"], "monitored_tokens")
		assert.EqInt(3, got["total_tokens"], "total_tokens")
	})
}
Esempio n. 19
0
func TestListApiTokens(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app, ts)
		// prepare
		d := lib.NewApiTokenDriver(appCtx)
		d.Issue("token1")
		d.Issue("token2")
		util.WaitFor(func() bool {
			c, _ := d.NewQuery().Count()
			return c == 2
		}, util.DefaultWaitForTimeout)

		// test
		var got []*models.ApiToken
		req := ts.Get("/api/admin/api_tokens/")
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)
		res.Json(&got)
		assert.EqInt(2, len(got), "ListApiToken length")
	})
}
Esempio n. 20
0
func TestRevokeApiToken(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 200
		req := ts.Delete(tokUrl)
		lib.SetApiTokenForTest(req, lib.Admin)
		res := req.RouteTo(app.Routes())
		assert.HttpStatus(200, res)
	})
}
Esempio n. 21
0
func TestListPost(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.App, ts)

		// TODO: fixture
		// prepare: creates 15 posts (5 for public, 5 for hidden, 5 for draft)
		d := NewPostDriver(appCtx)
		posts := make([]*blog.Post, 0)
		for i := 0; i < 5; i++ {
			posts = append(posts, &blog.Post{
				Title:     fmt.Sprintf("test post %d, public", i),
				Content:   "test content",
				Tags:      []string{},
				IsDraft:   false,
				IsHidden:  false,
				PublishAt: time.Now(),
			})
			posts = append(posts, &blog.Post{
				Title:     fmt.Sprintf("test post %d, draft", i),
				Content:   "test content",
				Tags:      []string{},
				IsDraft:   true,
				IsHidden:  false,
				PublishAt: time.Now(),
			})
			posts = append(posts, &blog.Post{
				Title:     fmt.Sprintf("test post %d, hidden", i),
				Content:   "test content",
				Tags:      []string{},
				IsDraft:   false,
				IsHidden:  true,
				PublishAt: time.Now(),
			})
		}
		d.SaveMulti(posts)

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

		// test : public post
		var got *postQueryResult
		var path = app.Api.Path("/posts/") + "?n=3"
		req := ts.Get(path)
		res := req.RouteTo(app.Routes())
		res.Json(&got)
		assert.HttpStatus(200, res)
		assert.EqInt(3, len(got.Posts), fmt.Sprintf("GET %s| num returned posts", path))
		assert.EqStr(app.Api.Path("/posts/")+"?page=1&n=3", got.Next, fmt.Sprintf("GET %s| next", path))
		assert.EqStr("", got.Previous, fmt.Sprintf("GET %s | previous", path))

		path = got.Next
		req = ts.Get(got.Next)
		res = req.RouteTo(app.Routes())
		res.Json(&got)
		assert.EqInt(2, len(got.Posts), fmt.Sprintf("GET %s | num returned posts", path))
		assert.EqStr("", got.Next, fmt.Sprintf("GET %s | next", path))
		assert.EqStr(app.Api.Path("/posts/")+"?page=0&n=3", got.Previous, fmt.Sprintf("GET %s | previous", path))

		// test : all posts visible to admin
		path = app.Api.Path("/posts/") + "?n=3&is_admin=true"
		req = ts.Get(path)
		lib.SetApiTokenForTest(req, lib.Admin)
		res = req.RouteTo(app.Routes())
		res.Json(&got)
		assert.HttpStatus(200, res)
		assert.EqInt(3, len(got.Posts), fmt.Sprintf("GET %s | num returned posts", path))
		assert.EqStr(app.Api.Path("/posts/")+"?page=1&n=3&is_admin=true", got.Next, fmt.Sprintf("GET %s | next", path))
		assert.EqStr("", got.Previous, fmt.Sprintf("GET %s | previous", path))

		path = app.Api.Path("/posts/") + "?page=5&n=3&is_admin=true"
		req = ts.Get(path)
		lib.SetApiTokenForTest(req, lib.Admin)
		res = req.RouteTo(app.Routes())
		res.Json(&got)
		assert.HttpStatus(200, res)
		assert.EqInt(0, len(got.Posts), fmt.Sprintf("GET %s | num returned posts", path))
		assert.EqStr("", got.Next, fmt.Sprintf("GET %s | next", path))
		assert.EqStr(app.Api.Path("/posts/")+"?page=4&n=3&is_admin=true", got.Previous, fmt.Sprintf("GET %s | previous", path))
	})
}
Esempio n. 22
0
func Test_updateContents(t *testing.T) {
	test.RunTestServer(func(ts *test.TestServer) {
		assert := test.NewAssert(t)
		appCtx := apptest.NewAppContextFromTestServer(app.TestApp().App, ts)

		var dummyMember = &ameblo.Member{}
		dummyMember.Name = "dummy"
		dummyMember.Nicknames = []string{"Updated"}

		amUrl := "http://ameblo.jp/foo/bar.html"
		d := NewAmebloEntryDriver(appCtx)
		refd := NewAmebloRefDriver(appCtx)

		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 := updateContents(
			appCtx,
			[]*ameblo.AmebloEntry{ent},
			[]*ameblo.Member{},
		)
		assert.Nil(err, "UpdateContents() 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.")
		c, _ := refd.NewQuery().Count()
		assert.EqInt(0, c, "AmebloRef should not be inserted by UpdateContents if it is empty.")

		// test confirm to update contents and refs
		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:    "(Updated) crawled content",
			AmLikes:    10,
			AmComments: 15,
		}
		err = updateContents(
			appCtx,
			[]*ameblo.AmebloEntry{newent},
			[]*ameblo.Member{dummyMember},
		)
		assert.Nil(err, "UpdateContents() 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.Content == "(Updated) crawled content"
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "Content should be updated after UpdateContents() within timeout window.")

		err = util.WaitFor(func() bool {
			c, _ = refd.NewQuery().Count()
			return c == 1
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "AmebloRef should be inserted after UpdateContents() within timeout window.")

		// test confirm to update contents and remove refs
		err = updateContents(
			appCtx,
			[]*ameblo.AmebloEntry{newent},
			[]*ameblo.Member{},
		)

		err = util.WaitFor(func() bool {
			c, _ = refd.NewQuery().Count()
			return c == 0
		}, util.DefaultWaitForTimeout)
		assert.Nil(err, "AmebloRef should be removed after UpdateContents() within timeout window.")

	})
}