discordgo_test.go 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. package discordgo_test
  2. import (
  3. "os"
  4. "runtime"
  5. "testing"
  6. "time"
  7. . "github.com/bwmarrin/discordgo"
  8. )
  9. //////////////////////////////////////////////////////////////////////////////
  10. ////////////////////////////////////////////////////// VARS NEEDED FOR TESTING
  11. var (
  12. dg *Session // Stores global discordgo session
  13. envToken string = os.Getenv("DG_TOKEN") // Token to use when authenticating
  14. envUsername string = os.Getenv("DG_USERNAME") // Username to use when authenticating
  15. envPassword string = os.Getenv("DG_PASSWORD") // Password to use when authenticating
  16. envGuild string = os.Getenv("DG_GUILD") // Guild ID to use for tests
  17. envChannel string = os.Getenv("DG_CHANNEL") // Channel ID to use for tests
  18. envUser string = os.Getenv("DG_USER") // User ID to use for tests
  19. envAdmin string = os.Getenv("DG_ADMIN") // User ID of admin user to use for tests
  20. )
  21. //////////////////////////////////////////////////////////////////////////////
  22. //////////////////////////////////////////// HELPER FUNCTIONS USED FOR TESTING
  23. // This waits x time for the check bool to be the want bool
  24. func waitBoolEqual(timeout time.Duration, check *bool, want bool) bool {
  25. start := time.Now()
  26. for {
  27. if *check == want {
  28. return true
  29. }
  30. if time.Since(start) > timeout {
  31. return false
  32. }
  33. runtime.Gosched()
  34. }
  35. }
  36. //////////////////////////////////////////////////////////////////////////////
  37. /////////////////////////////////////////////////////////////// START OF TESTS
  38. // TestNew tests the New() function without any arguments. This should return
  39. // a valid Session{} struct and no errors.
  40. func TestNew(t *testing.T) {
  41. _, err := New()
  42. if err != nil {
  43. t.Errorf("New() returned error: %+v", err)
  44. }
  45. }
  46. // TestNewUserPass tests the New() function with a username and password.
  47. // This should return a valid Session{}, a valid Session.Token, and open
  48. // a websocket connection to Discord.
  49. func TestNewUserPass(t *testing.T) {
  50. if envUsername == "" || envPassword == "" {
  51. t.Skip("Skipping New(username,password), DG_USERNAME or DG_PASSWORD not set")
  52. return
  53. }
  54. // Not testing yet.
  55. }
  56. // TestNewToken tests the New() function with a Token. This should return
  57. // the same as the TestNewUserPass function.
  58. func TestNewToken(t *testing.T) {
  59. if envToken == "" {
  60. t.Skip("Skipping New(token), DG_TOKEN not set")
  61. return
  62. }
  63. d, err := New(envToken)
  64. if err != nil {
  65. t.Fatalf("New(envToken) returned error: %+v", err)
  66. }
  67. if d == nil {
  68. t.Fatal("New(envToken), d is nil, should be Session{}")
  69. }
  70. if d.Token == "" {
  71. t.Fatal("New(envToken), d.Token is empty, should be a valid Token.")
  72. }
  73. if !waitBoolEqual(10*time.Second, &d.DataReady, true) {
  74. t.Fatal("New(envToken), d.DataReady is false after 10 seconds. Should be true.")
  75. }
  76. t.Log("Successfully connected to Discord.")
  77. dg = d
  78. }