Go client for the Write.as API https://developers.write.as
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
3.6 KiB

  1. package writeas
  2. import (
  3. "fmt"
  4. "net/http"
  5. "strings"
  6. "testing"
  7. "time"
  8. )
  9. func TestGetCollection(t *testing.T) {
  10. wac := NewClient()
  11. res, err := wac.GetCollection("blog")
  12. if err != nil {
  13. t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
  14. } else {
  15. t.Logf("Collection: %+v", res)
  16. if res.Title != "write.as" {
  17. t.Errorf("Unexpected fetch results: %+v\n", res)
  18. }
  19. }
  20. }
  21. func TestGetCollectionPosts(t *testing.T) {
  22. wac := NewClient()
  23. res, err := wac.GetCollectionPosts("blog")
  24. if err != nil {
  25. t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
  26. } else {
  27. if len(*res) == 0 {
  28. t.Errorf("No posts returned!")
  29. }
  30. }
  31. }
  32. func TestGetUserCollections(t *testing.T) {
  33. wac := NewDevClient()
  34. _, err := wac.LogIn("demo", "demo")
  35. if err != nil {
  36. t.Fatalf("Unable to log in: %v", err)
  37. }
  38. defer wac.LogOut()
  39. res, err := wac.GetUserCollections()
  40. if err != nil {
  41. t.Errorf("Unexpected fetch results: %+v, err: %v\n", res, err)
  42. } else {
  43. t.Logf("User collections: %+v", res)
  44. if len(*res) == 0 {
  45. t.Errorf("No collections returned!")
  46. }
  47. }
  48. }
  49. func TestCreateAndDeleteCollection(t *testing.T) {
  50. wac := NewDevClient()
  51. _, err := wac.LogIn("demo", "demo")
  52. if err != nil {
  53. t.Fatalf("Unable to log in: %v", err)
  54. }
  55. defer wac.LogOut()
  56. now := time.Now().Unix()
  57. alias := fmt.Sprintf("test-collection-%v", now)
  58. c, err := wac.CreateCollection(&CollectionParams{
  59. Alias: alias,
  60. Title: fmt.Sprintf("Test Collection %v", now),
  61. })
  62. if err != nil {
  63. t.Fatalf("Unable to create collection %q: %v", alias, err)
  64. }
  65. p := &DeleteCollectionParams{Alias: c.Alias}
  66. if err := wac.DeleteCollection(p); err != nil {
  67. t.Fatalf("Unable to delete collection %q: %v", alias, err)
  68. }
  69. }
  70. func TestDeleteCollectionUnauthenticated(t *testing.T) {
  71. wac := NewDevClient()
  72. now := time.Now().Unix()
  73. alias := fmt.Sprintf("test-collection-does-not-exist-%v", now)
  74. p := &DeleteCollectionParams{Alias: alias}
  75. err := wac.DeleteCollection(p)
  76. if err == nil {
  77. t.Fatalf("Should not be able to delete collection %q unauthenticated.", alias)
  78. }
  79. if !strings.Contains(err.Error(), "Not authenticated") {
  80. t.Fatalf("Error message should be more informative: %v", err)
  81. }
  82. }
  83. func ExampleClient_GetCollection() {
  84. c := NewClient()
  85. coll, err := c.GetCollection("blog")
  86. if err != nil {
  87. fmt.Printf("%v", err)
  88. return
  89. }
  90. fmt.Printf("%s", coll.Title)
  91. // Output: write.as
  92. }
  93. func TestCollectPostsAnonymous(t *testing.T) {
  94. // Create a post anonymously.
  95. wac := NewDevClient()
  96. p, err := wac.CreatePost(&PostParams{
  97. Title: "Title!",
  98. Content: "This is a post.",
  99. Font: "sans",
  100. })
  101. if err != nil {
  102. t.Errorf("Post create failed: %v", err)
  103. return
  104. }
  105. t.Logf("Post created: %+v", p)
  106. // Log in.
  107. if _, err := wac.LogIn("demo", "demo"); err != nil {
  108. t.Fatalf("Unable to log in: %v", err)
  109. }
  110. defer wac.LogOut()
  111. now := time.Now().Unix()
  112. alias := fmt.Sprintf("test-collection-%v", now)
  113. // Create a collection.
  114. _, err = wac.CreateCollection(&CollectionParams{
  115. Alias: alias,
  116. Title: fmt.Sprintf("Test Collection %v", now),
  117. })
  118. if err != nil {
  119. t.Fatalf("Unable to create collection %q: %v", alias, err)
  120. }
  121. defer wac.DeleteCollection(&DeleteCollectionParams{Alias: alias})
  122. // Move the anonymous post to this collection.
  123. res, err := wac.CollectPosts(&CollectPostParams{
  124. Alias: alias,
  125. Posts: []*CollectPost{
  126. {
  127. ID: p.ID,
  128. Token: p.Token,
  129. },
  130. },
  131. })
  132. if err != nil {
  133. t.Fatalf("Could not collect post %q: %v", p.ID, err)
  134. }
  135. for _, cr := range res {
  136. if cr.Code != http.StatusOK {
  137. t.Errorf("Failed to move post: %v", cr.ErrorMessage)
  138. } else {
  139. t.Logf("Moved post %q", cr.Post.ID)
  140. }
  141. }
  142. }