discordgo_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. // Checks if we're connected to Discord
  37. func isConnected() bool {
  38. if dg == nil {
  39. return false
  40. }
  41. if dg.Token == "" {
  42. return false
  43. }
  44. // Need a way to see if the ws connection is nil
  45. if !waitBoolEqual(10*time.Second, &dg.DataReady, true) {
  46. return false
  47. }
  48. return true
  49. }
  50. //////////////////////////////////////////////////////////////////////////////
  51. /////////////////////////////////////////////////////////////// START OF TESTS
  52. // TestNew tests the New() function without any arguments. This should return
  53. // a valid Session{} struct and no errors.
  54. func TestNew(t *testing.T) {
  55. _, err := New()
  56. if err != nil {
  57. t.Errorf("New() returned error: %+v", err)
  58. }
  59. }
  60. // TestNewUserPass tests the New() function with a username and password.
  61. // This should return a valid Session{}, a valid Session.Token, and open
  62. // a websocket connection to Discord.
  63. func TestNewUserPass(t *testing.T) {
  64. if isConnected() {
  65. t.Skip("Skipping New(username,password), already connected.")
  66. }
  67. if envUsername == "" || envPassword == "" {
  68. t.Skip("Skipping New(username,password), DG_USERNAME or DG_PASSWORD not set")
  69. return
  70. }
  71. // Not testing yet.
  72. }
  73. // TestNewToken tests the New() function with a Token. This should return
  74. // the same as the TestNewUserPass function.
  75. func TestNewToken(t *testing.T) {
  76. if isConnected() {
  77. t.Skip("Skipping New(token), already connected.")
  78. }
  79. if envToken == "" {
  80. t.Skip("Skipping New(token), DG_TOKEN not set")
  81. }
  82. d, err := New(envToken)
  83. if err != nil {
  84. t.Fatalf("New(envToken) returned error: %+v", err)
  85. }
  86. if d == nil {
  87. t.Fatal("New(envToken), d is nil, should be Session{}")
  88. }
  89. if d.Token == "" {
  90. t.Fatal("New(envToken), d.Token is empty, should be a valid Token.")
  91. }
  92. if !waitBoolEqual(10*time.Second, &d.DataReady, true) {
  93. t.Fatal("New(envToken), d.DataReady is false after 10 seconds. Should be true.")
  94. }
  95. t.Log("Successfully connected to Discord.")
  96. dg = d
  97. }