discord_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. package discordgo
  2. import (
  3. "os"
  4. "runtime"
  5. "testing"
  6. "time"
  7. )
  8. //////////////////////////////////////////////////////////////////////////////
  9. ////////////////////////////////////////////////////// VARS NEEDED FOR TESTING
  10. var (
  11. dg *Session // Stores global discordgo session
  12. envToken string = os.Getenv("DG_TOKEN") // Token to use when authenticating
  13. envEmail string = os.Getenv("DG_EMAIL") // Email to use when authenticating
  14. envPassword string = os.Getenv("DG_PASSWORD") // Password to use when authenticating
  15. envGuild string = os.Getenv("DG_GUILD") // Guild ID to use for tests
  16. envChannel string = os.Getenv("DG_CHANNEL") // Channel ID to use for tests
  17. envUser string = os.Getenv("DG_USER") // User ID to use for tests
  18. envAdmin string = os.Getenv("DG_ADMIN") // User ID of admin user to use for tests
  19. )
  20. //////////////////////////////////////////////////////////////////////////////
  21. //////////////////////////////////////////// HELPER FUNCTIONS USED FOR TESTING
  22. // This waits x time for the check bool to be the want bool
  23. func waitBoolEqual(timeout time.Duration, check *bool, want bool) bool {
  24. start := time.Now()
  25. for {
  26. if *check == want {
  27. return true
  28. }
  29. if time.Since(start) > timeout {
  30. return false
  31. }
  32. runtime.Gosched()
  33. }
  34. }
  35. // Checks if we're connected to Discord
  36. func isConnected() bool {
  37. if dg == nil {
  38. return false
  39. }
  40. if dg.Token == "" {
  41. return false
  42. }
  43. // Need a way to see if the ws connection is nil
  44. if !waitBoolEqual(10*time.Second, &dg.DataReady, true) {
  45. return false
  46. }
  47. return true
  48. }
  49. //////////////////////////////////////////////////////////////////////////////
  50. /////////////////////////////////////////////////////////////// START OF TESTS
  51. // TestNew tests the New() function without any arguments. This should return
  52. // a valid Session{} struct and no errors.
  53. func TestNew(t *testing.T) {
  54. _, err := New()
  55. if err != nil {
  56. t.Errorf("New() returned error: %+v", err)
  57. }
  58. }
  59. // TestInvalidToken tests the New() function with an invalid token
  60. func TestInvalidToken(t *testing.T) {
  61. d, err := New("asjkldhflkjasdh")
  62. if err != nil {
  63. t.Fatalf("New(InvalidToken) returned error: %+v", err)
  64. }
  65. if err = d.OpenAndListen(); err == nil {
  66. t.Fatalf("New(InvalidToken), d.OpenAndListen did not fail.")
  67. }
  68. }
  69. // TestInvalidUserPass tests the New() function with an invalid Email and Pass
  70. func TestInvalidEmailPass(t *testing.T) {
  71. _, err := New("invalidemail", "invalidpassword")
  72. if err == nil {
  73. t.Errorf("New(InvalidEmail, InvalidPass) returned nil error.")
  74. }
  75. }
  76. // TestInvalidPass tests the New() function with an invalid Password
  77. func TestInvalidPass(t *testing.T) {
  78. if envEmail == "" {
  79. t.Skip("Skipping New(username,InvalidPass), DG_EMAIL not set")
  80. return
  81. }
  82. _, err := New(envEmail, "invalidpassword")
  83. if err == nil {
  84. t.Errorf("New(Email, InvalidPass) returned nil error.")
  85. }
  86. }
  87. // TestNewUserPass tests the New() function with a username and password.
  88. // This should return a valid Session{}, a valid Session.Token, and open
  89. // a websocket connection to Discord.
  90. func TestNewUserPass(t *testing.T) {
  91. if envEmail == "" || envPassword == "" {
  92. t.Skip("Skipping New(username,password), DG_EMAIL or DG_PASSWORD not set")
  93. return
  94. }
  95. d, err := New(envEmail, envPassword)
  96. if err != nil {
  97. t.Fatalf("New(user,pass) returned error: %+v", err)
  98. }
  99. if d == nil {
  100. t.Fatal("New(user,pass), d is nil, should be Session{}")
  101. }
  102. if d.Token == "" {
  103. t.Fatal("New(user,pass), d.Token is empty, should be a valid Token.")
  104. }
  105. if err = d.OpenAndListen(); err != nil {
  106. t.Fatalf("New(user,pass), d.OpenAndListen failed: %+v", err)
  107. }
  108. if !waitBoolEqual(10*time.Second, &d.DataReady, true) {
  109. t.Fatal("New(user,pass), d.DataReady is false after 10 seconds. Should be true.")
  110. }
  111. t.Log("Successfully connected to Discord via New(user,pass).")
  112. dg = d
  113. if envToken == "" {
  114. envToken = dg.Token
  115. }
  116. }
  117. // TestNewToken tests the New() function with a Token. This should return
  118. // the same as the TestNewUserPass function.
  119. func TestNewToken(t *testing.T) {
  120. if envToken == "" {
  121. t.Skip("Skipping New(token), DG_TOKEN not set")
  122. }
  123. d, err := New(envToken)
  124. if err != nil {
  125. t.Fatalf("New(envToken) returned error: %+v", err)
  126. }
  127. if d == nil {
  128. t.Fatal("New(envToken), d is nil, should be Session{}")
  129. }
  130. if d.Token == "" {
  131. t.Fatal("New(envToken), d.Token is empty, should be a valid Token.")
  132. }
  133. if err = d.OpenAndListen(); err != nil {
  134. t.Fatalf("New(envToken), d.OpenAndListen failed: %+v", err)
  135. }
  136. if !waitBoolEqual(10*time.Second, &d.DataReady, true) {
  137. t.Fatal("New(envToken), d.DataReady is false after 10 seconds. Should be true.")
  138. }
  139. t.Log("Successfully connected to Discord via New(token).")
  140. dg = d
  141. }
  142. func TestClose(t *testing.T) {
  143. if envToken == "" {
  144. t.Skip("Skipping TestClose, DG_TOKEN not set")
  145. }
  146. d, err := New(envToken)
  147. if err != nil {
  148. t.Fatalf("TestClose, New(envToken) returned error: %+v", err)
  149. }
  150. if err = d.OpenAndListen(); err != nil {
  151. t.Fatalf("TestClose, d.OpenAndListen failed: %+v", err)
  152. }
  153. if err = d.Close(); err != nil {
  154. t.Fatalf("TestClose, d.Close failed: %+v", err)
  155. }
  156. }