Go client for the Write.as API https://developers.write.as

collection_test.go 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. package writeas
  2. import (
  3. "fmt"
  4. "strings"
  5. "testing"
  6. "time"
  7. )
  8. func TestGetCollection(t *testing.T) {
  9. dwac := NewDevClient()
  10. res, err := dwac.GetCollection("tester")
  11. if err != nil {
  12. t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
  13. }
  14. if res == nil {
  15. t.Error("Expected collection to not be nil")
  16. }
  17. }
  18. func TestGetCollectionPosts(t *testing.T) {
  19. dwac := NewDevClient()
  20. posts := []Post{}
  21. t.Run("Get all posts in collection", func(t *testing.T) {
  22. res, err := dwac.GetCollectionPosts("tester")
  23. if err != nil {
  24. t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
  25. }
  26. if len(*res) == 0 {
  27. t.Error("Expected at least on post in collection")
  28. }
  29. posts = *res
  30. })
  31. t.Run("Get one post from collection", func(t *testing.T) {
  32. res, err := dwac.GetCollectionPost("tester", posts[0].Slug)
  33. if err != nil {
  34. t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
  35. }
  36. if res == nil {
  37. t.Errorf("No post returned!")
  38. }
  39. if len(res.Content) == 0 {
  40. t.Errorf("Post content is empty!")
  41. }
  42. })
  43. }
  44. func TestGetUserCollections(t *testing.T) {
  45. wac := NewDevClient()
  46. _, err := wac.LogIn("demo", "demo")
  47. if err != nil {
  48. t.Fatalf("Unable to log in: %v", err)
  49. }
  50. defer wac.LogOut()
  51. res, err := wac.GetUserCollections()
  52. if err != nil {
  53. t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
  54. } else {
  55. t.Logf("User collections: %+v", res)
  56. if len(*res) == 0 {
  57. t.Errorf("No collections returned!")
  58. }
  59. }
  60. }
  61. func TestCreateAndDeleteCollection(t *testing.T) {
  62. wac := NewDevClient()
  63. _, err := wac.LogIn("demo", "demo")
  64. if err != nil {
  65. t.Fatalf("Unable to log in: %v", err)
  66. }
  67. defer wac.LogOut()
  68. now := time.Now().Unix()
  69. alias := fmt.Sprintf("test-collection-%v", now)
  70. c, err := wac.CreateCollection(&CollectionParams{
  71. Alias: alias,
  72. Title: fmt.Sprintf("Test Collection %v", now),
  73. })
  74. if err != nil {
  75. t.Fatalf("Unable to create collection %q: %v", alias, err)
  76. }
  77. if err := wac.DeleteCollection(c.Alias); err != nil {
  78. t.Fatalf("Unable to delete collection %q: %v", alias, err)
  79. }
  80. }
  81. func TestDeleteCollectionUnauthenticated(t *testing.T) {
  82. wac := NewDevClient()
  83. now := time.Now().Unix()
  84. alias := fmt.Sprintf("test-collection-does-not-exist-%v", now)
  85. err := wac.DeleteCollection(alias)
  86. if err == nil {
  87. t.Fatalf("Should not be able to delete collection %q unauthenticated.", alias)
  88. }
  89. if !strings.Contains(err.Error(), "Not authenticated") {
  90. t.Fatalf("Error message should be more informative: %v", err)
  91. }
  92. }