tests.go 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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://github.com/miniflux/miniflux/commits/master.atom"
  19. testFeedTitle = "Recent Commits to miniflux:master"
  20. testWebsiteURL = "https://github.com/miniflux/miniflux/commits/master"
  21. )
  22. func getRandomUsername() string {
  23. rand.Seed(time.Now().UnixNano())
  24. var suffix []string
  25. for i := 0; i < 10; i++ {
  26. suffix = append(suffix, strconv.Itoa(rand.Intn(1000)))
  27. }
  28. return "user" + strings.Join(suffix, "")
  29. }
  30. func createClient(t *testing.T) *miniflux.Client {
  31. username := getRandomUsername()
  32. client := miniflux.New(testBaseURL, testAdminUsername, testAdminPassword)
  33. _, err := client.CreateUser(username, testStandardPassword, false)
  34. if err != nil {
  35. t.Fatal(err)
  36. }
  37. return miniflux.New(testBaseURL, username, testStandardPassword)
  38. }
  39. func createFeed(t *testing.T, client *miniflux.Client) (*miniflux.Feed, *miniflux.Category) {
  40. categories, err := client.Categories()
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. feedID, err := client.CreateFeed(testFeedURL, categories[0].ID)
  45. if err != nil {
  46. t.Fatal(err)
  47. }
  48. if feedID == 0 {
  49. t.Fatalf(`Invalid feed ID, got %q`, feedID)
  50. }
  51. feed, err := client.Feed(feedID)
  52. if err != nil {
  53. t.Fatal(err)
  54. }
  55. return feed, categories[0]
  56. }