Commit a460ff02 authored by Morgan McMillian's avatar Morgan McMillian

added tests and fixed doc refs

parent 907e1703
......@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
## [Unreleased]
### Added
- Unit tests for existing functions
### Fixed
- pnut.io API documentation urls in comments
## [0.1.0] - 2019-01-13
### Added
......
package woodstock
import (
"encoding/json"
"os"
"testing"
)
const Delay = 15
type Config struct {
ClientID string
AccessToken string
}
func GetConfig() (config Config, err error) {
file, err := os.Open("config.json")
if err != nil {
return
}
decoder := json.NewDecoder(file)
err = decoder.Decode(&config)
if err != nil {
return
}
return
}
func TestAccessToken(t *testing.T) {
// TODO sort out a proper set of auth tests
t.Logf("skipping for now...")
}
avatar.png

14.8 KB

This diff is collapsed.
package woodstock
import (
"testing"
"time"
)
func TestGetChannel(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
channelID := "1001"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
channel, err := client.GetChannel(channelID)
if err != nil {
t.Error(err)
}
if channel.Data.ID != channelID {
t.Errorf("Channel ID doesn't match, got: %s, want: %s", channel.Data.ID, channelID)
}
time.Sleep(Delay)
}
func TestGetChannels(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
channelIDs := []string{"1000", "1001"}
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
channels, err := client.GetChannels(channelIDs)
if err != nil {
t.Error(err)
}
if channels.Data[0].ID != channelIDs[0] {
t.Errorf("Channel doesn't match, got %s, want %s", channels.Data[0].ID, channelIDs[0])
}
if channels.Data[1].ID != channelIDs[1] {
t.Errorf("Channel doesn't match, got %s, want %s", channels.Data[1].ID, channelIDs[1])
}
time.Sleep(Delay)
}
func TestCreateChannel(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
//payload := `{'type':'com.example.channel','acl':{'full':{'immutable':false,'you':true,'user_ids':[]},'write':{'any_user':true,'immutable':false,'you':true,'user_ids':[]},'read':{'any_user':true,'immutable':false,'you':true,'user_ids':[]}}}`
var acl ACL
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
channel, err := client.CreateChannel("com.example.channel", acl)
if err != nil {
t.Error(err)
}
t.Log(channel.Meta)
time.Sleep(Delay)
}
func TestUpdateChannel(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
channelID := "1333"
acl := ACL{
Read: Read{Public: true},
Write: Write{AnyUser: true},
}
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
channel, err := client.UpdateChannel(channelID, acl)
if err != nil {
t.Error(err)
}
t.Log(channel.Meta)
time.Sleep(Delay)
}
func TestDeleteChannel(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
var acl ACL
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
newchannel, err := client.CreateChannel("com.example.channel", acl)
if err != nil {
t.Error(err)
}
channel, err := client.DeleteChannel(newchannel.Data.ID)
if err != nil {
t.Error(err)
}
t.Log(channel.Meta)
time.Sleep(Delay)
}
func TestGetSubscribersOfChannel(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
channelID := "600"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
subs, err := client.GetSubscribersOfChannel(channelID)
if err != nil {
t.Error(err)
}
t.Log(subs.Meta)
time.Sleep(Delay)
}
func TestSubscribeChannel(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
channelID := "178"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
channel, err := client.SubscribeChannel(channelID)
if err != nil {
t.Error(err)
}
if channel.Data.YouSubscribed != true {
t.Errorf("YouSubscribed is not true")
}
time.Sleep(Delay)
}
func TestUnSubscribeChannel(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
channelID := "178"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
channel, err := client.UnSubscribeChannel(channelID)
if err != nil {
t.Error(err)
}
if channel.Data.YouSubscribed != false {
t.Errorf("YouSubscribed is not false")
}
time.Sleep(Delay)
}
func TestMuteChannel(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
channelID := "178"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
channel, err := client.MuteChannel(channelID)
if err != nil {
t.Error(err)
}
if channel.Data.YouMuted != true {
t.Errorf("YouMuted is not true")
}
time.Sleep(Delay)
}
func TestUnMuteChannel(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
channelID := "178"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
channel, err := client.UnMuteChannel(channelID)
if err != nil {
t.Error(err)
}
if channel.Data.YouMuted != false {
t.Errorf("YouMuted is not false")
}
time.Sleep(Delay)
}
func TestGetMessage(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
channelID := "178"
messageID := "106786"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
message, err := client.GetMessage(channelID, messageID)
if err != nil {
t.Error(err)
}
if message.Data.ID != messageID {
t.Errorf("Got the wrong message, got: %s, want: %s", message.Data.ID, messageID)
}
time.Sleep(Delay)
}
func TestGetMessagesInThread(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
channelID := "178"
messageID := "106786"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
messages, err := client.GetMessagesInThread(channelID, messageID)
if err != nil {
t.Error(err)
}
t.Log(messages.Meta)
time.Sleep(Delay)
}
func TestGetMessages(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
messageIDs := []string{"106786", "106785"}
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
messages, err := client.GetMessages(messageIDs)
if err != nil {
t.Error(err)
}
t.Log(messages.Meta)
time.Sleep(Delay)
}
func TestGetMessagesOfChannel(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
channelID := "178"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
messages, err := client.GetMessagesOfChannel(channelID)
if err != nil {
t.Error(err)
}
t.Log(messages.Meta)
time.Sleep(Delay)
}
func TestCreateMessage(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
channelID := "1001"
text := "golang rocks!"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
message, err := client.CreateMessage(channelID, text)
if err != nil {
t.Error(err)
}
if message.Data.Content.Text != text {
t.Errorf("Message does not match, got: %s, want: %s", message.Data.Content.Text, text)
}
time.Sleep(Delay)
}
func TestDeleteMessage(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
// TODO need to put creaet into this when fixed
channelID := "178"
messageID := "106947"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
message, err := client.DeleteMessage(channelID, messageID)
if err != nil {
t.Error(err)
}
if message.Data.IsDeleted != true {
t.Errorf("Message does not show as deleted.")
}
time.Sleep(Delay)
}
func TestGetStickyMessages(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
channelID := "212"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
messages, err := client.GetStickyMessages(channelID)
if err != nil {
t.Error(err)
}
t.Log(messages.Meta)
time.Sleep(Delay)
}
func TestSticky(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
channelID := "1001"
messageID := "106951"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
message, err := client.Sticky(channelID, messageID)
if err != nil {
t.Error(err)
}
t.Log(message.Meta)
time.Sleep(Delay)
}
func TestUnSticky(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
channelID := "1001"
messageID := "106951"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
message, err := client.UnSticky(channelID, messageID)
if err != nil {
t.Error(err)
}
t.Log(message.Meta)
time.Sleep(Delay)
}
......@@ -7,7 +7,7 @@ type ClientInfoResult struct {
}
// GetClient retrieves client object based on the id
// https://pnut.io/docs/resources/clients#get-clients-id
// https://pnut.io/docs/api/resources/clients#get-clients-id
func (c *Client) GetClient(id string) (result ClientInfosResult, err error) {
responseCh := make(chan response)
c.queryQueue <- query{url: ClientAPI + "/" + id, data: &result, method: "GET", responseCh: responseCh}
......
package woodstock
import (
"testing"
"time"
)
func TestGetClient(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
idstring := "fcYLbaFw47KMtMGZ9OBn7CXFLtxAkqDB"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
res, err := client.GetClient(idstring)
if err != nil {
t.Error(err)
}
t.Log(res.Meta)
time.Sleep(Delay)
}
cover.png

387 KB

......@@ -25,7 +25,7 @@ type ActionsResult struct {
}
// GetPost retrieves a post
// https://pnut.io/docs/resources/posts/lookup#get-posts-id
// https://pnut.io/docs/api/resources/posts/lookup#get-posts-id
func (c *Client) GetPost(id string) (result PostResult, err error) {
responseCh := make(chan response)
c.queryQueue <- query{url: PostAPI + "/" + id, data: &result, method: "GET", responseCh: responseCh}
......@@ -33,7 +33,7 @@ func (c *Client) GetPost(id string) (result PostResult, err error) {
}
// GetPosts retrieves mutliple posts
// https://pnut.io/docs/resources/posts/lookup#get-posts
// https://pnut.io/docs/api/resources/posts/lookup#get-posts
func (c *Client) GetPosts(ids []string) (result PostsResult, err error) {
v := url.Values{}
v.Set("ids", strings.Join(ids, ","))
......@@ -43,7 +43,7 @@ func (c *Client) GetPosts(ids []string) (result PostsResult, err error) {
}
// GetPostRevisions retrieves a list of reviesions of a post
// https://pnut.io/docs/resources/posts/lookup#get-posts-id-revisions
// https://pnut.io/docs/api/resources/posts/lookup#get-posts-id-revisions
func (c *Client) GetPostRevisions(id string) (result PostsResult, err error) {
responseCh := make(chan response)
c.queryQueue <- query{url: PostAPI + "/" + id + "/revisions", data: &result, method: "GET", responseCh: responseCh}
......@@ -51,7 +51,7 @@ func (c *Client) GetPostRevisions(id string) (result PostsResult, err error) {
}
// Post creates a post
// https://pnut.io/docs/resources/posts/lifecycle#post-posts
// https://pnut.io/docs/api/resources/posts/lifecycle#post-posts
func (c *Client) Post(v url.Values) (result PostResult, err error) {
responseCh := make(chan response)
c.queryQueue <- query{url: PostAPI, form: v, data: &result, method: "POST", responseCh: responseCh}
......@@ -59,7 +59,7 @@ func (c *Client) Post(v url.Values) (result PostResult, err error) {
}
// RevisePost updates an existing post
// https://pnut.io/docs/resources/posts/lifecycle#put-posts-id
// https://pnut.io/docs/api/resources/posts/lifecycle#put-posts-id
func (c *Client) RevisePost(id string, v url.Values) (result PostResult, err error) {
responseCh := make(chan response)
c.queryQueue <- query{url: PostAPI + "/" + id, form: v, data: &result, method: "PUT", responseCh: responseCh}
......@@ -67,7 +67,7 @@ func (c *Client) RevisePost(id string, v url.Values) (result PostResult, err err
}
// DeletePost deletes a post
// https://pnut.io/docs/resources/posts/lifecycle#delete-posts-id
// https://pnut.io/docs/api/resources/posts/lifecycle#delete-posts-id
func (c *Client) DeletePost(id string) (result PostResult, err error) {
responseCh := make(chan response)
c.queryQueue <- query{url: PostAPI + "/" + id, data: &result, method: "DELETE", responseCh: responseCh}
......@@ -75,7 +75,7 @@ func (c *Client) DeletePost(id string) (result PostResult, err error) {
}
// MeStream retrieves a personal stream
// https://pnut.io/docs/resources/posts/streams#get-posts-streams-me
// https://pnut.io/docs/api/resources/posts/streams#get-posts-streams-me
func (c *Client) MeStream(count ...int) (result PostsResult, err error) {
v := url.Values{}
if len(count) > 0 {
......@@ -91,7 +91,7 @@ func (c *Client) MeStream(count ...int) (result PostsResult, err error) {
}
// UnifiedStream retrieves a unified stream
// https://pnut.io/docs/resources/posts/streams#get-posts-streams-unified
// https://pnut.io/docs/api/resources/posts/streams#get-posts-streams-unified
func (c *Client) UnifiedStream(count ...int) (result PostsResult, err error) {
v := url.Values{}
if len(count) > 0 {
......@@ -107,7 +107,7 @@ func (c *Client) UnifiedStream(count ...int) (result PostsResult, err error) {
}
// GlobalStream retrieves the global stream
// https://pnut.io/docs/resources/posts/streams#get-posts-streams-unified
// https://pnut.io/docs/api/resources/posts/streams#get-posts-streams-unified
func (c *Client) GlobalStream(count ...int) (result PostsResult, err error) {
v := url.Values{}
if len(count) > 0 {
......@@ -123,7 +123,7 @@ func (c *Client) GlobalStream(count ...int) (result PostsResult, err error) {
}
// TagStream retrieves a stream for a given tag
// https://pnut.io/docs/resources/posts/streams#get-posts-tag-tag
// https://pnut.io/docs/api/resources/posts/streams#get-posts-tag-tag
func (c *Client) TagStream(tag string, count ...int) (result PostsResult, err error) {
v := url.Values{}
if len(count) > 0 {
......@@ -139,7 +139,7 @@ func (c *Client) TagStream(tag string, count ...int) (result PostsResult, err er
}
// GetThread retrieves post in a given thread
// https://pnut.io/docs/resources/posts/threads#get-posts-id-thread
// https://pnut.io/docs/api/resources/posts/threads#get-posts-id-thread
func (c *Client) GetThread(id string) (result PostsResult, err error) {
responseCh := make(chan response)
c.queryQueue <- query{url: PostAPI + "/" + id + "/thread", data: &result, method: "GET", responseCh: responseCh}
......@@ -147,7 +147,7 @@ func (c *Client) GetThread(id string) (result PostsResult, err error) {
}
// Repost repeats a post
// https://pnut.io/docs/resources/posts/reposts#put-posts-id-repost
// https://pnut.io/docs/api/resources/posts/reposts#put-posts-id-repost
func (c *Client) Repost(id string) (result PostResult, err error) {
responseCh := make(chan response)
c.queryQueue <- query{url: PostAPI + "/" + id + "/repost", data: &result, method: "PUT", responseCh: responseCh}
......@@ -155,7 +155,7 @@ func (c *Client) Repost(id string) (result PostResult, err error) {
}
// UnRepost removes a repeated post
// https://pnut.io/docs/resources/posts/reposts#delete-posts-id-repost
// https://pnut.io/docs/api/resources/posts/reposts#delete-posts-id-repost
func (c *Client) UnRepost(id string) (result PostResult, err error) {
responseCh := make(chan response)
c.queryQueue <- query{url: PostAPI + "/" + id + "/repost", data: &result, method: "DELETE", responseCh: responseCh}
......@@ -163,7 +163,7 @@ func (c *Client) UnRepost(id string) (result PostResult, err error) {
}
// Bookmark creates a bookmark (stars) a post
// https://pnut.io/docs/resources/posts/bookmarks#put-posts-id-bookmark
// https://pnut.io/docs/api/resources/posts/bookmarks#put-posts-id-bookmark
func (c *Client) Bookmark(id string) (result PostResult, err error) {
responseCh := make(chan response)
c.queryQueue <- query{url: PostAPI + "/" + id + "/bookmark", data: &result, method: "PUT", responseCh: responseCh}
......@@ -171,7 +171,7 @@ func (c *Client) Bookmark(id string) (result PostResult, err error) {
}
// UnBookmark deletes a bookmark (unstars) a post
// https://pnut.io/docs/resources/posts/bookmarks#put-posts-id-bookmark
// https://pnut.io/docs/api/resources/posts/bookmarks#put-posts-id-bookmark
func (c *Client) UnBookmark(id string) (result PostResult, err error) {
responseCh := make(chan response)
c.queryQueue <- query{url: PostAPI + "/" + id + "/bookmark", data: &result, method: "DELETE", responseCh: responseCh}
......@@ -179,7 +179,7 @@ func (c *Client) UnBookmark(id string) (result PostResult, err error) {
}
// GetActions retrieves actions
// https://pnut.io/docs/resources/posts/actions#get-posts-id-actions
// https://pnut.io/docs/api/resources/posts/actions#get-posts-id-actions
func (c *Client) GetActions(id string, v ...url.Values) (result ActionsResult, err error) {
param := ""
if len(v) > 0 {
......
package woodstock
import (
"net/url"
"testing"
"time"
)
func TestPost(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
text := "Hello pnut.io"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
v := url.Values{}
v.Set("text", text)
post, err := client.Post(v)
if err != nil {
t.Error(err)
}
if post.Data.Content.Text != text {
t.Errorf("Post appears incorrect, got: %s, want: %s", post.Data.Content.Text, text)
}
time.Sleep(Delay)
}
func TestGetPost(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
postID := "541893"
text := "Hello pnut.io"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
post, err := client.GetPost(postID)
if err != nil {
t.Error(err)
}
if post.Data.Content.Text != text {
t.Errorf("Post appears incorrect, got: %s, want: %s", post.Data.Content.Text, text)
}
time.Sleep(Delay)
}
func TestGetPosts(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
postIDs := []string{"1", "2", "3"}
text1 := "So, West Virginia--my birth state--they changed the slogan on the interstate signs to \"Open for business\" for a while. -Ross\n\n#quoteSunday"
text2 := "posting test"
text3 := "Hello World\n"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
posts, err := client.GetPosts(postIDs)
if err != nil {
t.Error(err)
}
if posts.Data[0].Content.Text != text1 {
t.Errorf("Post appears incorrect, got: %s, want: %s", posts.Data[0].Content.Text, text1)
}
if posts.Data[1].Content.Text != text2 {
t.Errorf("Post appears incorrect, got: %s, want: %s", posts.Data[1].Content.Text, text2)
}
if posts.Data[2].Content.Text != text3 {
t.Errorf("Post appears incorrect, got: %s, want: %s", posts.Data[2].Content.Text, text3)
}
time.Sleep(Delay)
}
func TestGetPostRevisions(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
postID := "544502"
text := "Blarp"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
posts, err := client.GetPostRevisions(postID)
if err != nil {
t.Error(err)
}
if posts.Data[0].Content.Text != text {
t.Errorf("Post appears incorrect, got: %s, want: %s", posts.Data[0].Content.Text, text)
}
time.Sleep(Delay)
}
func TestRevisePost(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
text := "Hello pnut.io"
new := "Hello pnut.io golang test"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
v := url.Values{}
v.Set("text", text)
post, err := client.Post(v)
if err != nil {
t.Error(err)
}
v.Set("text", new)
rpost, rerr := client.RevisePost(post.Data.ID, v)
if rerr != nil {
t.Error(rerr)
}
if rpost.Data.Content.Text != new {
t.Errorf("Revised post appears incorrect, got: %s, want: %s", post.Data.Content.Text, new)
}
time.Sleep(Delay)
}
func TestDeletePost(t *testing.T) {
config, err := GetConfig()
if err != nil {
t.Error(err)
}
text := "Hello pnut.io"
client := NewClient(config.ClientID, "")
client.SetAccessToken(config.AccessToken)
v := url.Values{}
v.Set("text", text)
post, err := client.Post(v)
if err != nil {
t.Error(err)
}
dpost, derr := client.DeletePost(post.Data.ID)
if derr != nil {
t.Error(derr)
}