tests.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2018 Frédéric Guillot. All rights reserved.
  2. // Use of this source code is governed by the Apache 2.0
  3. // license that can be found in the LICENSE file.
  4. package tests
  5. import (
  6. "math/rand"
  7. "strconv"
  8. "strings"
  9. "testing"
  10. "time"
  11. miniflux "miniflux.app/client"
  12. )
  13. const (
  14. testBaseURL = "http://127.0.0.1:8080/"
  15. testAdminUsername = "admin"
  16. testAdminPassword = "test123"
  17. testStandardPassword = "secret"
  18. testFeedURL = "https://miniflux.app/feed.xml"
  19. testFeedTitle = "Miniflux"
  20. testSubscriptionTitle = "Miniflux Releases"
  21. testWebsiteURL = "https://miniflux.app/"
  22. )
  23. func getRandomUsername() string {
  24. rand.Seed(time.Now().UnixNano())
  25. var suffix []string
  26. for i := 0; i < 10; i++ {
  27. suffix = append(suffix, strconv.Itoa(rand.Intn(1000)))
  28. }
  29. return "user" + strings.Join(suffix, "")
  30. }
  31. func createClient(t *testing.T) *miniflux.Client {
  32. username := getRandomUsername()
  33. client := miniflux.New(testBaseURL, testAdminUsername, testAdminPassword)
  34. _, err := client.CreateUser(username, testStandardPassword, false)
  35. if err != nil {
  36. t.Fatal(err)
  37. }
  38. return miniflux.New(testBaseURL, username, testStandardPassword)
  39. }
  40. func createFeed(t *testing.T, client *miniflux.Client) (*miniflux.Feed, *miniflux.Category) {
  41. categories, err := client.Categories()
  42. if err != nil {
  43. t.Fatal(err)
  44. }
  45. feedID, err := client.CreateFeed(&miniflux.FeedCreationRequest{
  46. FeedURL: testFeedURL,
  47. CategoryID: categories[0].ID,
  48. })
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. if feedID == 0 {
  53. t.Fatalf(`Invalid feed ID, got %q`, feedID)
  54. }
  55. feed, err := client.Feed(feedID)
  56. if err != nil {
  57. t.Fatal(err)
  58. }
  59. return feed, categories[0]
  60. }