discord_test.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. func init() {
  21. if envEmail == "" || envPassword == "" || envToken == "" {
  22. return
  23. }
  24. if d, err := New(envEmail, envPassword, envToken); err == nil {
  25. dg = d
  26. }
  27. }
  28. //////////////////////////////////////////////////////////////////////////////
  29. //////////////////////////////////////////// HELPER FUNCTIONS USED FOR TESTING
  30. // This waits x time for the check bool to be the want bool
  31. func waitBoolEqual(timeout time.Duration, check *bool, want bool) bool {
  32. start := time.Now()
  33. for {
  34. if *check == want {
  35. return true
  36. }
  37. if time.Since(start) > timeout {
  38. return false
  39. }
  40. runtime.Gosched()
  41. }
  42. }
  43. // Checks if we're connected to Discord
  44. func isConnected() bool {
  45. if dg == nil {
  46. return false
  47. }
  48. if dg.Token == "" {
  49. return false
  50. }
  51. // Need a way to see if the ws connection is nil
  52. if !waitBoolEqual(10*time.Second, &dg.DataReady, true) {
  53. return false
  54. }
  55. return true
  56. }
  57. //////////////////////////////////////////////////////////////////////////////
  58. /////////////////////////////////////////////////////////////// START OF TESTS
  59. // TestNew tests the New() function without any arguments. This should return
  60. // a valid Session{} struct and no errors.
  61. func TestNew(t *testing.T) {
  62. _, err := New()
  63. if err != nil {
  64. t.Errorf("New() returned error: %+v", err)
  65. }
  66. }
  67. // TestInvalidToken tests the New() function with an invalid token
  68. func TestInvalidToken(t *testing.T) {
  69. _, err := New("asjkldhflkjasdh")
  70. if err != nil {
  71. t.Fatalf("New(InvalidToken) returned error: %+v", err)
  72. }
  73. }
  74. // TestInvalidUserPass tests the New() function with an invalid Email and Pass
  75. func TestInvalidEmailPass(t *testing.T) {
  76. _, err := New("invalidemail", "invalidpassword")
  77. if err == nil {
  78. t.Errorf("New(InvalidEmail, InvalidPass) returned nil error.")
  79. }
  80. }
  81. // TestInvalidPass tests the New() function with an invalid Password
  82. func TestInvalidPass(t *testing.T) {
  83. if envEmail == "" {
  84. t.Skip("Skipping New(username,InvalidPass), DG_EMAIL not set")
  85. return
  86. }
  87. _, err := New(envEmail, "invalidpassword")
  88. if err == nil {
  89. t.Errorf("New(Email, InvalidPass) returned nil error.")
  90. }
  91. }
  92. // TestNewUserPass tests the New() function with a username and password.
  93. // This should return a valid Session{}, a valid Session.Token, and open
  94. // a websocket connection to Discord.
  95. func TestNewUserPass(t *testing.T) {
  96. if envEmail == "" || envPassword == "" {
  97. t.Skip("Skipping New(username,password), DG_EMAIL or DG_PASSWORD not set")
  98. return
  99. }
  100. d, err := New(envEmail, envPassword)
  101. if err != nil {
  102. t.Fatalf("New(user,pass) returned error: %+v", err)
  103. }
  104. if d == nil {
  105. t.Fatal("New(user,pass), d is nil, should be Session{}")
  106. }
  107. if d.Token == "" {
  108. t.Fatal("New(user,pass), d.Token is empty, should be a valid Token.")
  109. }
  110. }
  111. // TestNewToken tests the New() function with a Token. This should return
  112. // the same as the TestNewUserPass function.
  113. func TestNewToken(t *testing.T) {
  114. if envToken == "" {
  115. t.Skip("Skipping New(token), DG_TOKEN not set")
  116. }
  117. d, err := New(envToken)
  118. if err != nil {
  119. t.Fatalf("New(envToken) returned error: %+v", err)
  120. }
  121. if d == nil {
  122. t.Fatal("New(envToken), d is nil, should be Session{}")
  123. }
  124. if d.Token == "" {
  125. t.Fatal("New(envToken), d.Token is empty, should be a valid Token.")
  126. }
  127. }
  128. func TestOpenClose(t *testing.T) {
  129. if envToken == "" {
  130. t.Skip("Skipping TestClose, DG_TOKEN not set")
  131. }
  132. d, err := New(envToken)
  133. if err != nil {
  134. t.Fatalf("TestClose, New(envToken) returned error: %+v", err)
  135. }
  136. if err = d.OpenAndListen(); err != nil {
  137. t.Fatalf("TestClose, d.OpenAndListen failed: %+v", err)
  138. }
  139. if err = d.Close(); err != nil {
  140. t.Fatalf("TestClose, d.Close failed: %+v", err)
  141. }
  142. }