Go client for the Write.as API https://developers.write.as
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

112 linhas
2.6 KiB

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