snoobert/comment_test.go
Vartan Benohanian 7922711d51 Handle listings better by using custom unmarshaling
Signed-off-by: Vartan Benohanian <vartanbeno@gmail.com>
2020-05-03 17:35:06 -04:00

216 lines
6.2 KiB
Go

package geddit
import (
"fmt"
"net/http"
"net/url"
"reflect"
"testing"
"time"
)
var expectedCommentSubmitOrEdit = &Comment{
ID: "test2",
FullID: "t1_test2",
ParentID: "t1_test",
Permalink: "/r/subreddit/comments/test1/some_thread/test2/",
Body: "test comment",
Author: "reddit_username",
AuthorID: "t2_user1",
AuthorFlairText: "Flair",
Subreddit: "subreddit",
SubredditNamePrefixed: "r/subreddit",
SubredditID: "t5_test",
Score: 1,
Controversiality: 0,
Created: &Timestamp{time.Date(2020, 4, 28, 20, 9, 47, 0, time.UTC)},
LinkID: "t3_link1",
}
func TestCommentServiceOp_Submit(t *testing.T) {
setup()
defer teardown()
commentBlob := readFileContents(t, "testdata/comment-submit-edit.json")
mux.HandleFunc("/api/comment", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
form := url.Values{}
form.Set("api_type", "json")
form.Set("return_rtjson", "true")
form.Set("parent", "t1_test")
form.Set("text", "test comment")
_ = r.ParseForm()
if expect, actual := form, r.PostForm; !reflect.DeepEqual(expect, actual) {
t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual))
}
fmt.Fprint(w, commentBlob)
})
comment, _, err := client.Comment.Submit(ctx, "t1_test", "test comment")
if err != nil {
t.Fatalf("got unexpected error: %v", err)
}
if expect, actual := expectedCommentSubmitOrEdit, comment; !reflect.DeepEqual(expect, actual) {
t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual))
}
}
func TestCommentServiceOp_Edit(t *testing.T) {
setup()
defer teardown()
commentBlob := readFileContents(t, "testdata/comment-submit-edit.json")
mux.HandleFunc("/api/editusertext", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
form := url.Values{}
form.Set("api_type", "json")
form.Set("return_rtjson", "true")
form.Set("thing_id", "t1_test")
form.Set("text", "test comment")
_ = r.ParseForm()
if expect, actual := form, r.PostForm; !reflect.DeepEqual(expect, actual) {
t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual))
}
fmt.Fprint(w, commentBlob)
})
_, _, err := client.Comment.Edit(ctx, "t3_test", "test comment")
if err == nil {
t.Fatal("expected error, got nothing instead")
}
if expect, actual := `must provide comment id (starting with t1_); id provided: "t3_test"`, err.Error(); expect != actual {
t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual))
}
comment, _, err := client.Comment.Edit(ctx, "t1_test", "test comment")
if err != nil {
t.Fatalf("got unexpected error: %v", err)
}
if expect, actual := expectedCommentSubmitOrEdit, comment; !reflect.DeepEqual(expect, actual) {
t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual))
}
}
func TestCommentServiceOp_Delete(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/api/del", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
form := url.Values{}
form.Set("id", "t1_test")
_ = r.ParseForm()
if expect, actual := form, r.PostForm; !reflect.DeepEqual(expect, actual) {
t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual))
}
fmt.Fprint(w, `{}`)
})
_, err := client.Comment.Delete(ctx, "t3_test")
if err == nil {
t.Fatal("expected error, got nothing instead")
}
if expect, actual := `must provide comment id (starting with t1_); id provided: "t3_test"`, err.Error(); expect != actual {
t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual))
}
res, err := client.Comment.Delete(ctx, "t1_test")
if err != nil {
t.Fatalf("got unexpected error: %v", err)
}
if expect, actual := http.StatusOK, res.StatusCode; expect != actual {
t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual))
}
}
func TestCommentServiceOp_Save(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/api/save", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
form := url.Values{}
form.Set("id", "t1_test")
_ = r.ParseForm()
if expect, actual := form, r.PostForm; !reflect.DeepEqual(expect, actual) {
t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual))
}
fmt.Fprint(w, `{}`)
})
_, err := client.Comment.Save(ctx, "t3_test")
if err == nil {
t.Fatal("expected error, got nothing instead")
}
if expect, actual := `must provide comment id (starting with t1_); id provided: "t3_test"`, err.Error(); expect != actual {
t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual))
}
res, err := client.Comment.Save(ctx, "t1_test")
if err != nil {
t.Fatalf("got unexpected error: %v", err)
}
if expect, actual := http.StatusOK, res.StatusCode; expect != actual {
t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual))
}
}
func TestCommentServiceOp_Unsave(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/api/unsave", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
form := url.Values{}
form.Set("id", "t1_test")
_ = r.ParseForm()
if expect, actual := form, r.PostForm; !reflect.DeepEqual(expect, actual) {
t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual))
}
fmt.Fprint(w, `{}`)
})
_, err := client.Comment.Unsave(ctx, "t3_test")
if err == nil {
t.Fatal("expected error, got nothing instead")
}
if expect, actual := `must provide comment id (starting with t1_); id provided: "t3_test"`, err.Error(); expect != actual {
t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual))
}
res, err := client.Comment.Unsave(ctx, "t1_test")
if err != nil {
t.Fatalf("got unexpected error: %v", err)
}
if expect, actual := http.StatusOK, res.StatusCode; expect != actual {
t.Fatalf("got unexpected value\nexpect: %s\nactual: %s", Stringify(expect), Stringify(actual))
}
}