Skip to content

Commit

Permalink
Adding follow/unfollow + clarifying tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrien Musset committed Jun 27, 2016
1 parent 1d33caf commit 7e059d6
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 126 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ WIP [getstream.io](getstream.io) client in pure GO.

* Authentication
* Add, list all, and remove an activity.
* Follows/unfollows.

# YET TO WORK

* Listing/paging options.
* Follows/unfollows.
* Subscription.
* Aggregated feeds support.
* Notification feeds support.
* multiple adding, follow
* Listing/paging options.
* Subscription. ??
* Aggregated feeds support. == related to listing, paging
* Notification feeds support. == get read seen ??

# NICE TO HAVE

Expand Down
2 changes: 1 addition & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func (c *Client) Feed(slug, id string) *Feed {
}
}

func (c *Client) get(result interface{}, path string, slug Slug) error {
func (c *Client) get(result interface{}, path string, slug Slug, opt *Options) error {
return c.request(result, "GET", path, slug, nil)
}

Expand Down
15 changes: 4 additions & 11 deletions client_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package getstream_test
package getstream

import (
"testing"
// . "github.com/hyperworks/go-getstream"
a "github.com/stretchr/testify/assert"

"github.com/stretchr/testify/assert"
)

func TestClient_BaseURL(t *testing.T) {
Expand All @@ -15,13 +15,6 @@ func TestClient_BaseURL(t *testing.T) {

for location, url := range locations {
client := ConnectTestClient(location)
a.Equal(t, url, client.BaseURL().String())
assert.Equal(t, url, client.BaseURL().String())
}
}

func TestClient_Feed(t *testing.T) {
client := ConnectTestClient("")
feed := client.Feed(TestFeedSlug.Slug, TestFeedSlug.ID)
a.Equal(t, TestFeedSlug.WithToken(TestToken), feed.Slug())
a.Equal(t, TestFeedSignature, feed.Slug().Signature())
}
36 changes: 30 additions & 6 deletions feed.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package getstream

import ()

type Feed struct {
*Client
slug Slug
Expand All @@ -24,12 +22,13 @@ func (f *Feed) AddActivities(activities []*Activity) error {
}

// TODO: A result type to recieve the listing result.

panic("not yet implemented.")
}

func (f *Feed) Activities(opt *Options) ([]*Activity, error) {
result := ActivitiesResult{}
e := f.get(&result, f.url(), f.slug)
e := f.get(&result, f.url(), f.slug, opt)
return result.Results, e
}

Expand All @@ -38,17 +37,42 @@ func (f *Feed) RemoveActivity(id string) error {
}

func (f *Feed) Follow(feed, id string) error {
panic("not implemented.")

followedSlug := Slug{feed, id, ""}
signedFollowingSlug := SignSlug(f.secret, f.slug)
data := Follow{Target: followedSlug.String()}
e := f.post(nil, f.url()+"following/", signedFollowingSlug, data)
return e
}

func (f *Feed) Unfollow(feed, id string) error {
panic("not implemented.")
return f.del(f.url()+"following/"+feed+":"+id+"/", f.slug)
}

func (f *Feed) Following(opt *Options) ([]*Feed, error) {
result := FollowersResult{}
e := f.get(&result, f.url()+"following/", f.slug, nil)
return result.Results, e
}

func (f *Feed) Followers(opt *Options) ([]*Feed, error) {
panic("not implemented.")
result := FollowersResult{}
e := f.get(&result, f.url()+"followers/", f.slug, nil)
return result.Results, e
}

func (f *Feed) url() string {
return "feed/" + f.slug.Slug + "/" + f.slug.ID + "/"
}

func (f *Feed) Empty() error {
activities, e := f.Activities(nil)

if e == nil {
for _, activity := range activities {
f.RemoveActivity(activity.ID)
}
}

return e
}
91 changes: 66 additions & 25 deletions feed_test.go
Original file line number Diff line number Diff line change
@@ -1,40 +1,81 @@
package getstream_test
package getstream

import (
"testing"
// . "github.com/hyperworks/go-getstream"
a "github.com/stretchr/testify/assert"

"github.com/stretchr/testify/assert"
)

func TestFeed(t *testing.T) {
if testing.Short() {
t.Skip()
}

client := ConnectTestClient("")
feed := client.Feed(TestFeedSlug.Slug, TestFeedSlug.ID)
client := ConnectTestClient("eu-west")
feed := client.Feed("user", "john2")
activity := NewTestActivity()

t.Log("adding activity...")
addedActivity, e := feed.AddActivity(activity)
a.NoError(t, e)
a.NotEqual(t, activity, addedActivity, "AddActivity should not modify existing instance.")
a.NotNil(t, addedActivity)
a.NotEmpty(t, addedActivity.ID)
assert.Nil(t, e)
assert.NotEqual(t, activity, addedActivity, "AddActivity should not modify existing instance.")
assert.NotNil(t, addedActivity)
assert.NotEmpty(t, addedActivity.ID)

t.Log("listing added activities...")
activities, e := feed.Activities(nil)
a.NoError(t, e)
a.NotEmpty(t, activities)
a.Len(t, activities, 1) // otherwise we might be getting result from another test run.
a.Equal(t, addedActivity.ID, activities[0].ID)
assert.NoError(t, e)
assert.NotEmpty(t, activities)
assert.Len(t, activities, 1) // otherwise we might be getting result from another test run.
assert.Equal(t, addedActivity.ID, activities[0].ID)

t.Log("removing added activity...")
e = feed.RemoveActivity(addedActivity.ID)
a.NoError(t, e)
assert.NoError(t, e)

t.Log("listing added activities again...")
activities, e = feed.Activities(nil)
a.NoError(t, e)
a.Empty(t, activities)
assert.NoError(t, e)
assert.Empty(t, activities)

}

func TestFollow(t *testing.T) {
client := ConnectTestClient("eu-west")
john := client.Feed("user", "john2")
timeline := client.Feed("Test", "test3")
activity := NewTestActivity()
addedActivity, e := timeline.AddActivity(activity)
assert.Nil(t, e)

e = john.Follow("Test", "test3")
assert.NoError(t, e)
activities, e := john.Activities(nil)
assert.NoError(t, e)
assert.NotEmpty(t, activities)
assert.Len(t, activities, 1) // otherwise we might be getting result from another test run.
assert.Equal(t, addedActivity.ID, activities[0].ID)

following, e := john.Following(nil)
assert.NoError(t, e)
assert.NotEmpty(t, following)
assert.Len(t, following, 1) // otherwise we might be getting result from another test run.

followers, e := timeline.Followers(nil)
assert.NoError(t, e)
assert.NotEmpty(t, followers)
assert.Len(t, followers, 1) // otherwise we might be getting result from another test run.

e = john.Unfollow("Test", "test3")
assert.NoError(t, e)
activities, e = john.Activities(nil)
assert.NoError(t, e)
assert.Empty(t, activities)

following, e = john.Following(nil)
assert.NoError(t, e)
assert.Empty(t, following)

followers, e = timeline.Followers(nil)
assert.NoError(t, e)
assert.Empty(t, followers)

e = timeline.RemoveActivity(addedActivity.ID)
assert.NoError(t, e)

}

func TestOptions(t *testing.T) {

}
30 changes: 6 additions & 24 deletions shared_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,11 @@
package getstream_test
package getstream

import (
. "github.com/hyperworks/go-getstream"
"os"
)

const (
TestToken = "xfHQwFY2MreP7bTGsoxQ7s2ebLA"
TestTargetFeedToken = "3ZIhnU1vw524lXGsOd7wb0DkmrU"
TestTargetFeedToken2 = "IKjr7WED0O3ROLZGEIFrCSwBo4Y"

TestFeedSignature = "userflat14483198-3e43-4a91-a2ed-fc88dcf2fd7b " + TestToken
TestTargetFeedSignature = "userflat72d8ee6c-c27c-49a5-9311-c5e0e67356e5 " + TestTargetFeedToken
TestTargetFeedSignature2 = "userflate1f8917c-e6dd-4d06-b6ff-59805d8e2b96 " + TestTargetFeedToken2
)
import "os"

var (
TestAPIKey = os.Getenv("GETSTREAM_KEY")
TestAPISecret = os.Getenv("GETSTREAM_SECRET")
TestAppID = os.Getenv("GETSTREAM_APPID")

TestFeedSlug = Slug{"userflat", "14483198-3e43-4a91-a2ed-fc88dcf2fd7b", ""}
TestTargetFeedSlug = Slug{"userflat", "72d8ee6c-c27c-49a5-9311-c5e0e67356e5", ""}
TestTargetFeedSlug2 = Slug{"userflat", "e1f8917c-e6dd-4d06-b6ff-59805d8e2b96", ""}
TestObjectSlug = Slug{"testobject", "2109704a-e048-4f5c-b534-1ff8322b8ae9", ""}
)

func ConnectTestClient(region string) *Client {
Expand All @@ -32,9 +14,9 @@ func ConnectTestClient(region string) *Client {

func NewTestActivity() *Activity {
return &Activity{
Actor: TestFeedSlug,
Verb: "comment",
Object: TestObjectSlug,
To: []Slug{TestTargetFeedSlug, TestTargetFeedSlug2},
Actor: Slug{"user", "john", ""},
Verb: "add",
ObjectData: ObjectData{ID: "id", Name: "object"},
Object: Slug{"object", "id", ""},
}
}
27 changes: 2 additions & 25 deletions sign_test.go
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
package getstream_test
package getstream

import (
. "github.com/hyperworks/go-getstream"
a "github.com/stretchr/testify/assert"
"testing"
)

func TestSign(t *testing.T) {
result := Sign(TestAPISecret, TestFeedSlug.Slug+TestFeedSlug.ID)
a.Equal(t, TestToken, result)
}

func TestSignSlug(t *testing.T) {
expected := TestFeedSlug.WithToken(TestToken)
actual := SignSlug(TestAPISecret, TestFeedSlug)
a.Equal(t, expected, actual)
a.Equal(t, TestFeedSignature, actual.Signature())
}

func TestSignActivity(t *testing.T) {
act := NewTestActivity()
act = SignActivity(TestAPISecret, act)
a.Equal(t, TestTargetFeedSignature, act.To[0].Signature())
a.Equal(t, TestTargetFeedSignature2, act.To[1].Signature())
}
//TODO
23 changes: 12 additions & 11 deletions slug_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
package getstream_test
package getstream

import (
"encoding/json"
"fmt"
. "github.com/hyperworks/go-getstream"
a "github.com/stretchr/testify/assert"

"testing"

"github.com/stretchr/testify/assert"
)

var _ json.Marshaler = Slug{}
var _ json.Unmarshaler = &Slug{}
var _ fmt.Stringer = Slug{}

func TestSlug(t *testing.T) {
a.Equal(t, "test:456", Slug{"test", "456", ""}.String())
a.Equal(t, "test:123 token", Slug{"test", "123", "token"}.String())
assert.Equal(t, "test:456", Slug{"test", "456", ""}.String())
assert.Equal(t, "test:123 token", Slug{"test", "123", "token"}.String())
}

func TestSlug_Valid(t *testing.T) {
Expand All @@ -24,7 +25,7 @@ func TestSlug_Valid(t *testing.T) {
}

for _, slug := range valids {
a.True(t, slug.Valid())
assert.True(t, slug.Valid())
}

invalids := []Slug{
Expand All @@ -35,7 +36,7 @@ func TestSlug_Valid(t *testing.T) {
}

for _, slug := range invalids {
a.False(t, slug.Valid())
assert.False(t, slug.Valid())
}
}

Expand All @@ -47,8 +48,8 @@ func TestSlug_JSON(t *testing.T) {

for slug, str := range marshals {
bytes, e := json.Marshal(slug)
a.NoError(t, e, "failed to marshal slug: "+slug.String())
a.Equal(t, str, string(bytes))
assert.NoError(t, e, "failed to marshal slug: "+slug.String())
assert.Equal(t, str, string(bytes))
}

unmarshals := map[string]Slug{
Expand All @@ -60,7 +61,7 @@ func TestSlug_JSON(t *testing.T) {
for str, slug := range unmarshals {
result := Slug{}
e := json.Unmarshal([]byte(str), &result)
a.NoError(t, e, "failed to unmarshal json: "+str)
a.Equal(t, slug, result)
assert.NoError(t, e, "failed to unmarshal json: "+str)
assert.Equal(t, slug, result)
}
}
Loading

0 comments on commit 7e059d6

Please sign in to comment.