discord_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. _, err := New("asjkldhflkjasdh")
  62. if err == nil {
  63. t.Errorf("New(InvalidToken) returned nil error.")
  64. }
  65. }
  66. // TestInvalidUserPass tests the New() function with an invalid Email and Pass
  67. func TestInvalidEmailPass(t *testing.T) {
  68. _, err := New("invalidemail", "invalidpassword")
  69. if err == nil {
  70. t.Errorf("New(InvalidEmail, InvalidPass) returned nil error.")
  71. }
  72. }
  73. // TestInvalidPass tests the New() function with an invalid Password
  74. func TestInvalidPass(t *testing.T) {
  75. if envEmail == "" {
  76. t.Skip("Skipping New(username,InvalidPass), DG_USERNAME not set")
  77. return
  78. }
  79. _, err := New(envEmail, "invalidpassword")
  80. if err == nil {
  81. t.Errorf("New(Email, InvalidPass) returned nil error.")
  82. }
  83. }
  84. // TestNewUserPass tests the New() function with a username and password.
  85. // This should return a valid Session{}, a valid Session.Token, and open
  86. // a websocket connection to Discord.
  87. func TestNewUserPass(t *testing.T) {
  88. if envEmail == "" || envPassword == "" {
  89. t.Skip("Skipping New(username,password), DG_USERNAME or DG_PASSWORD not set")
  90. return
  91. }
  92. d, err := New(envEmail, envPassword)
  93. if err != nil {
  94. t.Fatalf("New(user,pass) returned error: %+v", err)
  95. }
  96. if d == nil {
  97. t.Fatal("New(user,pass), d is nil, should be Session{}")
  98. }
  99. if d.Token == "" {
  100. t.Fatal("New(user,pass), d.Token is empty, should be a valid Token.")
  101. }
  102. if !waitBoolEqual(10*time.Second, &d.DataReady, true) {
  103. t.Fatal("New(user,pass), d.DataReady is false after 10 seconds. Should be true.")
  104. }
  105. t.Log("Successfully connected to Discord via New(user,pass).")
  106. dg = d
  107. // Not testing yet.
  108. }
  109. // TestNewToken tests the New() function with a Token. This should return
  110. // the same as the TestNewUserPass function.
  111. func TestNewToken(t *testing.T) {
  112. if envToken == "" {
  113. t.Skip("Skipping New(token), DG_TOKEN not set")
  114. }
  115. d, err := New(envToken)
  116. if err != nil {
  117. t.Fatalf("New(envToken) returned error: %+v", err)
  118. }
  119. if d == nil {
  120. t.Fatal("New(envToken), d is nil, should be Session{}")
  121. }
  122. if d.Token == "" {
  123. t.Fatal("New(envToken), d.Token is empty, should be a valid Token.")
  124. }
  125. if !waitBoolEqual(10*time.Second, &d.DataReady, true) {
  126. t.Fatal("New(envToken), d.DataReady is false after 10 seconds. Should be true.")
  127. }
  128. t.Log("Successfully connected to Discord via New(token).")
  129. dg = d
  130. }