discordgo_test.go 4.3 KB

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