Ejemplo n.º 1
0
func TestAuthFail(t *testing.T) {
	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter,
		r *http.Request) {
		http.Error(w, "Unauthorized", http.StatusUnauthorized)
	}))
	defer ts.Close()

	pinboard.BaseURL = ts.URL + "/%s/?%s"

	in := "bad:token"

	wantError := "HTTP 401 Unauthorized"

	pin := pinboard.New()

	got, err := pin.Auth(in)

	if err.Error() != wantError {
		t.Errorf("error: got %v want %s", err, wantError)
	}
	if got != "" {
		t.Errorf("auth: got %v want \"\" (empty string)", got)
	}
	if pin.Token() != "" {
		t.Errorf("auth: got %s want \"\" (empty string)", got)
	}
	if pin.IsAuthed() != false {
		t.Errorf("auth: got %t want false", pin.IsAuthed())
	}
}
Ejemplo n.º 2
0
func TestNoteID(t *testing.T) {
	ts := startTestServer()
	defer ts.Close()

	pin := pinboard.New()
	_, err := pin.Auth("mango:0123456789")
	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}
	got, err := pin.NoteID("1234")
	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}
	if got.Text != "some note text." {
		t.Errorf("id: got %v want \"some note text.\"", got.Text)
	}
}
Ejemplo n.º 3
0
func TestAuthSuccess(t *testing.T) {
	ts := startTestServer()
	defer ts.Close()

	in := "mango:0123456789"
	want := "0123456789"

	pin := pinboard.New()
	got, err := pin.Auth(in)

	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}

	if got != want {
		t.Errorf("auth: got %s want %s", got, want)
	}
}
Ejemplo n.º 4
0
func TestNotes(t *testing.T) {
	ts := startTestServer()
	defer ts.Close()

	pin := pinboard.New()

	_, err := pin.Auth("mango:0123456789")
	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}

	got, err := pin.Notes()
	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}
	if len(got) != 2 {
		t.Errorf("len: got %v want 2", got)
	}
}
Ejemplo n.º 5
0
func TestLastUpdate(t *testing.T) {
	ts := startTestServer()
	defer ts.Close()

	want := time.Date(2015, 7, 2, 17, 3, 45, 0, time.UTC)
	pin := pinboard.New()
	_, err := pin.Auth("mango:0123456789")
	if err != nil {
		t.Errorf("error: got %s want nil", err)
	}
	got, err := pin.LastUpdate()
	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}

	if got != want {
		t.Errorf("time: got %v want %v", got, want)
	}
}
Ejemplo n.º 6
0
func TestDel(t *testing.T) {
	ts := startTestServer()
	defer ts.Close()

	pin := pinboard.New()

	_, err := pin.Auth("mango:0123456789")
	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}

	want := true

	got, err := pin.Del("aazzbbzz")
	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}
	if got != want {
		t.Errorf("error: got %v want %v", got, want)
	}
}
Ejemplo n.º 7
0
func TestSuggest(t *testing.T) {
	ts := startTestServer()
	defer ts.Close()

	pin := pinboard.New()

	_, err := pin.Auth("mango:0123456789")
	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}

	wantPop := pinboard.Popular{"security"}

	wantRec := pinboard.Recommended{"privacy", "security", "internet", "politics",
		"eff", "freedom", "rights", "Technology", "Copyright", "opensource"}

	pop, rec, err := pin.Suggest("http://www.eef.org")

	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}
	if len(pop) != len(wantPop) {
		t.Errorf("popular len: got %v want %v", len(pop), len(wantPop))
	}
	if len(rec) != len(wantRec) {
		t.Errorf("rec len: got %v want %v", len(rec), len(wantRec))
	}
	for i := 0; i < len(pop); i++ {
		if pop[i] != wantPop[i] {
			t.Errorf("popular: got %v want %v", pop[i], wantPop[i])
		}
	}
	for i := 0; i < len(rec); i++ {
		if rec[i] != wantRec[i] {
			t.Errorf("rec: got %v want %v", rec[i], wantRec[i])
		}
	}
}
Ejemplo n.º 8
0
func TestDates(t *testing.T) {
	ts := startTestServer()
	defer ts.Close()

	pin := pinboard.New()
	_, err := pin.Auth("mango:0123456789")
	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}

	var want []pinboard.Post
	want = append(want, pinboard.Post{Date: time.Date(2015, 7, 3, 0, 0, 0, 0, time.UTC), Count: 4})
	want = append(want, pinboard.Post{Date: time.Date(2015, 7, 2, 0, 0, 0, 0, time.UTC), Count: 2})
	want = append(want, pinboard.Post{Date: time.Date(2015, 7, 1, 0, 0, 0, 0, time.UTC), Count: 1})

	tg := []string{"news"}

	got, err := pin.Dates(tg)

	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}

	checkPost := func(o pinboard.Post) bool {
		for _, s := range want {
			if s.Date == o.Date && s.Count == o.Count {
				return true
			}
		}
		return false
	}

	for _, s := range got {
		if !checkPost(s) {
			t.Errorf("dates: got %v want %v", got, want)
		}
	}
}
Ejemplo n.º 9
0
func TestRenTag(t *testing.T) {
	ts := startTestServer()
	defer ts.Close()

	pin := pinboard.New()
	_, err := pin.Auth("mango:0123456789")

	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}

	want := true

	got, err := pin.RenTag("foo", "bar")

	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}

	if got != want {
		t.Errorf("ren tag: got %v want %v", got, want)
	}
}
Ejemplo n.º 10
0
func TestAdd(t *testing.T) {
	ts := startTestServer()
	defer ts.Close()

	pin := pinboard.New()

	_, err := pin.Auth("mango:0123456789")

	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}

	tg := []string{"zza", "zzb", "zzc"}

	b := pinboard.Bookmark{
		URL:     "http://www.food.com/",
		Title:   "Foody",
		Desc:    "some food website",
		Tags:    tg,
		Created: time.Now(),
		Replace: true,
		Shared:  true,
		ToRead:  true,
	}

	want := true

	got, err := pin.Add(b)

	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}

	if got != want {
		t.Errorf("add: got %v want %v", got, want)
	}
}
Ejemplo n.º 11
0
func TestBookmarks(t *testing.T) {
	ts := startTestServer()
	defer ts.Close()

	pin := pinboard.New()

	_, err := pin.Auth("mango:0123456789")
	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}

	var want []pinboard.Bookmark
	tg := []string{"foo_tag"}

	b := pinboard.Bookmark{
		URL:     "https://foo.com",
		Title:   "foo desc",
		Desc:    "foo extended",
		Tags:    tg,
		Created: time.Date(2015, 7, 1, 12, 37, 35, 0, time.UTC),
		Meta:    []byte("0ed700a46ec65d5eaea9f95618dd66ad"),
		Hash:    []byte("4969cc1ba7205f7cd19376ae6930dfcc"),
		Shared:  false,
		ToRead:  false,
	}

	want = append(want, b)
	tg = []string{"bar_tag1", "bar_tag2"}

	b = pinboard.Bookmark{
		URL:     "https://bar.com",
		Title:   "bar desc",
		Desc:    "bar extended",
		Tags:    tg,
		Created: time.Date(2015, 6, 29, 8, 51, 22, 0, time.UTC),
		Meta:    []byte("eb96f3fa1d3f334ee544bc5cbc03c7fe"),
		Hash:    []byte("c0c2fd08d877b6ea9b4faf3657eeadbd"),
		Shared:  false,
		ToRead:  false,
	}

	want = append(want, b)

	got, err := pin.Bookmarks(tg, 0, 2, time.Time{}, time.Time{}, false)

	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}

	if len(got) != 2 {
		t.Errorf("len: got %v want 2", got)
	}

	err = compareBookmarks(got[0], want[0])
	if err != nil {
		t.Errorf("error: %v", err)
	}

	err = compareBookmarks(got[1], want[1])
	if err != nil {
		t.Errorf("error: %v", err)
	}
}
Ejemplo n.º 12
0
func TestRecent(t *testing.T) {
	ts := startTestServer()
	defer ts.Close()

	pin := pinboard.New()
	_, err := pin.Auth("mango:0123456789")
	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}

	var want []pinboard.Bookmark

	tg := []string{"aazzaa", "bbzzbb"}

	b := pinboard.Bookmark{
		URL:     "http://aaa.com/",
		Title:   "AAA",
		Desc:    "AAA",
		Tags:    tg,
		Created: time.Date(2015, 7, 2, 7, 56, 40, 0, time.UTC),
		Meta:    []byte("0feee4bcd1ee2724ef8b266c8baaa29c"),
		Hash:    []byte("d67b75105b042e87b54342de46aca979"),
		Shared:  false,
		ToRead:  true,
	}

	want = append(want, b)
	tg = []string{"aazzaa"}

	b = pinboard.Bookmark{
		URL:     "https://bbb.org/",
		Title:   "BBB",
		Desc:    "BBB",
		Tags:    tg,
		Created: time.Date(2015, 7, 2, 17, 3, 45, 0, time.UTC),
		Meta:    []byte("aca07c3d2676549f454129ffc47bdd3d"),
		Hash:    []byte("0356c24cf4856410e04a82289e57d79a"),
		Shared:  false,
		ToRead:  true,
	}
	want = append(want, b)

	got, err := pin.Recent(nil, 2)

	if err != nil {
		t.Errorf("error: got %v want nil", err)
	}

	if len(got) != 2 {
		t.Errorf("len: got %v want 2", got)
	}

	err = compareBookmarks(got[0], want[0])
	if err != nil {
		t.Errorf("error: %v", err)
	}

	err = compareBookmarks(got[1], want[1])
	if err != nil {
		t.Errorf("error: %v", err)
	}
}