discord_test.go 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. package discordgo
  2. import (
  3. "os"
  4. "runtime"
  5. "sync/atomic"
  6. "testing"
  7. "time"
  8. )
  9. //////////////////////////////////////////////////////////////////////////////
  10. ////////////////////////////////////////////////////// VARS NEEDED FOR TESTING
  11. var (
  12. dg *Session // Stores global discordgo session
  13. envToken = os.Getenv("DG_TOKEN") // Token to use when authenticating
  14. envEmail = os.Getenv("DG_EMAIL") // Email to use when authenticating
  15. envPassword = os.Getenv("DG_PASSWORD") // Password to use when authenticating
  16. envGuild = os.Getenv("DG_GUILD") // Guild ID to use for tests
  17. envChannel = os.Getenv("DG_CHANNEL") // Channel ID to use for tests
  18. // envUser = os.Getenv("DG_USER") // User ID to use for tests
  19. envAdmin = os.Getenv("DG_ADMIN") // User ID of admin user to use for tests
  20. )
  21. func init() {
  22. if envEmail == "" || envPassword == "" || envToken == "" {
  23. return
  24. }
  25. if d, err := New(envEmail, envPassword, envToken); err == nil {
  26. dg = d
  27. }
  28. }
  29. //////////////////////////////////////////////////////////////////////////////
  30. //////////////////////////////////////////// HELPER FUNCTIONS USED FOR TESTING
  31. // This waits x time for the check bool to be the want bool
  32. func waitBoolEqual(timeout time.Duration, check *bool, want bool) bool {
  33. start := time.Now()
  34. for {
  35. if *check == want {
  36. return true
  37. }
  38. if time.Since(start) > timeout {
  39. return false
  40. }
  41. runtime.Gosched()
  42. }
  43. }
  44. // Checks if we're connected to Discord
  45. func isConnected() bool {
  46. if dg == nil {
  47. return false
  48. }
  49. if dg.Token == "" {
  50. return false
  51. }
  52. // Need a way to see if the ws connection is nil
  53. if !waitBoolEqual(10*time.Second, &dg.DataReady, true) {
  54. return false
  55. }
  56. return true
  57. }
  58. //////////////////////////////////////////////////////////////////////////////
  59. /////////////////////////////////////////////////////////////// START OF TESTS
  60. // TestNew tests the New() function without any arguments. This should return
  61. // a valid Session{} struct and no errors.
  62. func TestNew(t *testing.T) {
  63. _, err := New()
  64. if err != nil {
  65. t.Errorf("New() returned error: %+v", err)
  66. }
  67. }
  68. // TestInvalidToken tests the New() function with an invalid token
  69. func TestInvalidToken(t *testing.T) {
  70. d, err := New("asjkldhflkjasdh")
  71. if err != nil {
  72. t.Fatalf("New(InvalidToken) returned error: %+v", err)
  73. }
  74. // New with just a token does not do any communication, so attempt an api call.
  75. _, err = d.UserSettings()
  76. if err == nil {
  77. t.Errorf("New(InvalidToken), d.UserSettings returned nil error.")
  78. }
  79. }
  80. // TestInvalidUserPass tests the New() function with an invalid Email and Pass
  81. func TestInvalidEmailPass(t *testing.T) {
  82. _, err := New("invalidemail", "invalidpassword")
  83. if err == nil {
  84. t.Errorf("New(InvalidEmail, InvalidPass) returned nil error.")
  85. }
  86. }
  87. // TestInvalidPass tests the New() function with an invalid Password
  88. func TestInvalidPass(t *testing.T) {
  89. if envEmail == "" {
  90. t.Skip("Skipping New(username,InvalidPass), DG_EMAIL not set")
  91. return
  92. }
  93. _, err := New(envEmail, "invalidpassword")
  94. if err == nil {
  95. t.Errorf("New(Email, InvalidPass) returned nil error.")
  96. }
  97. }
  98. // TestNewUserPass tests the New() function with a username and password.
  99. // This should return a valid Session{}, a valid Session.Token.
  100. func TestNewUserPass(t *testing.T) {
  101. if envEmail == "" || envPassword == "" {
  102. t.Skip("Skipping New(username,password), DG_EMAIL or DG_PASSWORD not set")
  103. return
  104. }
  105. d, err := New(envEmail, envPassword)
  106. if err != nil {
  107. t.Fatalf("New(user,pass) returned error: %+v", err)
  108. }
  109. if d == nil {
  110. t.Fatal("New(user,pass), d is nil, should be Session{}")
  111. }
  112. if d.Token == "" {
  113. t.Fatal("New(user,pass), d.Token is empty, should be a valid Token.")
  114. }
  115. }
  116. // TestNewToken tests the New() function with a Token. This should return
  117. // the same as the TestNewUserPass function.
  118. func TestNewToken(t *testing.T) {
  119. if envToken == "" {
  120. t.Skip("Skipping New(token), DG_TOKEN not set")
  121. }
  122. d, err := New(envToken)
  123. if err != nil {
  124. t.Fatalf("New(envToken) returned error: %+v", err)
  125. }
  126. if d == nil {
  127. t.Fatal("New(envToken), d is nil, should be Session{}")
  128. }
  129. if d.Token == "" {
  130. t.Fatal("New(envToken), d.Token is empty, should be a valid Token.")
  131. }
  132. }
  133. // TestNewUserPassToken tests the New() function with a username, password and token.
  134. // This should return the same as the TestNewUserPass function.
  135. func TestNewUserPassToken(t *testing.T) {
  136. if envEmail == "" || envPassword == "" || envToken == "" {
  137. t.Skip("Skipping New(username,password,token), DG_EMAIL, DG_PASSWORD or DG_TOKEN not set")
  138. return
  139. }
  140. d, err := New(envEmail, envPassword, envToken)
  141. if err != nil {
  142. t.Fatalf("New(user,pass,token) returned error: %+v", err)
  143. }
  144. if d == nil {
  145. t.Fatal("New(user,pass,token), d is nil, should be Session{}")
  146. }
  147. if d.Token == "" {
  148. t.Fatal("New(user,pass,token), d.Token is empty, should be a valid Token.")
  149. }
  150. }
  151. func TestOpenClose(t *testing.T) {
  152. if envToken == "" {
  153. t.Skip("Skipping TestClose, DG_TOKEN not set")
  154. }
  155. d, err := New(envToken)
  156. if err != nil {
  157. t.Fatalf("TestClose, New(envToken) returned error: %+v", err)
  158. }
  159. if err = d.Open(); err != nil {
  160. t.Fatalf("TestClose, d.Open failed: %+v", err)
  161. }
  162. if !waitBoolEqual(10*time.Second, &d.DataReady, true) {
  163. t.Fatal("DataReady never became true.")
  164. }
  165. // TODO find a better way
  166. // Add a small sleep here to make sure heartbeat and other events
  167. // have enough time to get fired. Need a way to actually check
  168. // those events.
  169. time.Sleep(2 * time.Second)
  170. // UpdateStatus - maybe we move this into wsapi_test.go but the websocket
  171. // created here is needed. This helps tests that the websocket was setup
  172. // and it is working.
  173. if err = d.UpdateStatus(0, time.Now().String()); err != nil {
  174. t.Errorf("UpdateStatus error: %+v", err)
  175. }
  176. if err = d.Close(); err != nil {
  177. t.Fatalf("TestClose, d.Close failed: %+v", err)
  178. }
  179. }
  180. func TestAddHandler(t *testing.T) {
  181. testHandlerCalled := int32(0)
  182. testHandler := func(s *Session, m *MessageCreate) {
  183. atomic.AddInt32(&testHandlerCalled, 1)
  184. }
  185. interfaceHandlerCalled := int32(0)
  186. interfaceHandler := func(s *Session, i interface{}) {
  187. atomic.AddInt32(&interfaceHandlerCalled, 1)
  188. }
  189. bogusHandlerCalled := false
  190. bogusHandler := func(s *Session, se *Session) {
  191. bogusHandlerCalled = true
  192. }
  193. d := Session{}
  194. d.AddHandler(testHandler)
  195. d.AddHandler(testHandler)
  196. d.AddHandler(interfaceHandler)
  197. d.AddHandler(bogusHandler)
  198. d.handle(&MessageCreate{})
  199. d.handle(&MessageDelete{})
  200. <-time.After(100 * time.Millisecond)
  201. // testHandler will be called twice because it was added twice.
  202. if testHandlerCalled != 2 {
  203. t.Fatalf("testHandler was not called twice.")
  204. }
  205. // interfaceHandler will be called twice, once for each event.
  206. if interfaceHandlerCalled != 2 {
  207. t.Fatalf("interfaceHandler was not called twice.")
  208. }
  209. if bogusHandlerCalled {
  210. t.Fatalf("bogusHandler was called.")
  211. }
  212. }
  213. func TestRemoveHandler(t *testing.T) {
  214. testHandlerCalled := int32(0)
  215. testHandler := func(s *Session, m *MessageCreate) {
  216. atomic.AddInt32(&testHandlerCalled, 1)
  217. }
  218. d := Session{}
  219. r := d.AddHandler(testHandler)
  220. d.handle(&MessageCreate{})
  221. r()
  222. d.handle(&MessageCreate{})
  223. <-time.After(100 * time.Millisecond)
  224. // testHandler will be called once, as it was removed in between calls.
  225. if testHandlerCalled != 1 {
  226. t.Fatalf("testHandler was not called once.")
  227. }
  228. }