restapi_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. package discordgo
  2. import (
  3. "testing"
  4. )
  5. //////////////////////////////////////////////////////////////////////////////
  6. /////////////////////////////////////////////////////////////// START OF TESTS
  7. // TestChannelMessageSend tests the ChannelMessageSend() function. This should not return an error.
  8. func TestChannelMessageSend(t *testing.T) {
  9. if envChannel == "" {
  10. t.Skip("Skipping, DG_CHANNEL not set.")
  11. }
  12. if dg == nil {
  13. t.Skip("Skipping, dg not set.")
  14. }
  15. _, err := dg.ChannelMessageSend(envChannel, "Running REST API Tests!")
  16. if err != nil {
  17. t.Errorf("ChannelMessageSend returned error: %+v", err)
  18. }
  19. }
  20. func TestUserAvatar(t *testing.T) {
  21. if dg == nil {
  22. t.Skip("Cannot TestUserAvatar, dg not set.")
  23. }
  24. a, err := dg.UserAvatar("@me")
  25. if err != nil {
  26. if err.Error() == `HTTP 404 NOT FOUND, {"message": ""}` {
  27. t.Skip("Skipped, @me doesn't have an Avatar")
  28. }
  29. t.Errorf(err.Error())
  30. }
  31. if a == nil {
  32. t.Errorf("a == nil, should be image.Image")
  33. }
  34. }
  35. /* Running this causes an error due to 2/hour rate limit on username changes
  36. func TestUserUpdate(t *testing.T) {
  37. if dg == nil {
  38. t.Skip("Cannot test logout, dg not set.")
  39. }
  40. u, err := dg.User("@me")
  41. if err != nil {
  42. t.Errorf(err.Error())
  43. }
  44. s, err := dg.UserUpdate(envEmail, envPassword, "testname", u.Avatar, "")
  45. if err != nil {
  46. t.Error(err.Error())
  47. }
  48. if s.Username != "testname" {
  49. t.Error("Username != testname")
  50. }
  51. s, err = dg.UserUpdate(envEmail, envPassword, u.Username, u.Avatar, "")
  52. if err != nil {
  53. t.Error(err.Error())
  54. }
  55. if s.Username != u.Username {
  56. t.Error("Username != " + u.Username)
  57. }
  58. }
  59. */
  60. //func (s *Session) UserChannelCreate(recipientID string) (st *Channel, err error) {
  61. func TestUserChannelCreate(t *testing.T) {
  62. if dg == nil {
  63. t.Skip("Cannot TestUserChannelCreate, dg not set.")
  64. }
  65. if envAdmin == "" {
  66. t.Skip("Skipped, DG_ADMIN not set.")
  67. }
  68. _, err := dg.UserChannelCreate(envAdmin)
  69. if err != nil {
  70. t.Errorf(err.Error())
  71. }
  72. // TODO make sure the channel was added
  73. }
  74. func TestUserChannels(t *testing.T) {
  75. if dg == nil {
  76. t.Skip("Cannot TestUserChannels, dg not set.")
  77. }
  78. _, err := dg.UserChannels()
  79. if err != nil {
  80. t.Errorf(err.Error())
  81. }
  82. }
  83. func TestUserGuilds(t *testing.T) {
  84. if dg == nil {
  85. t.Skip("Cannot TestUserGuilds, dg not set.")
  86. }
  87. _, err := dg.UserGuilds()
  88. if err != nil {
  89. t.Errorf(err.Error())
  90. }
  91. }
  92. func TestUserSettings(t *testing.T) {
  93. if dg == nil {
  94. t.Skip("Cannot TestUserSettings, dg not set.")
  95. }
  96. _, err := dg.UserSettings()
  97. if err != nil {
  98. t.Errorf(err.Error())
  99. }
  100. }
  101. // TestLogout tests the Logout() function. This should not return an error.
  102. func TestLogout(t *testing.T) {
  103. if dg == nil {
  104. t.Skip("Cannot TestLogout, dg not set.")
  105. }
  106. err := dg.Logout()
  107. if err != nil {
  108. t.Errorf("Logout() returned error: %+v", err)
  109. }
  110. }
  111. func TestGateway(t *testing.T) {
  112. _, err := dg.Gateway()
  113. if err != nil {
  114. t.Errorf("Gateway() returned error: %+v", err)
  115. }
  116. }
  117. func TestVoiceICE(t *testing.T) {
  118. if dg == nil {
  119. t.Skip("Skipping, dg not set.")
  120. }
  121. _, err := dg.VoiceICE()
  122. if err != nil {
  123. t.Errorf("VoiceICE() returned error: %+v", err)
  124. }
  125. }
  126. func TestVoiceRegions(t *testing.T) {
  127. if dg == nil {
  128. t.Skip("Skipping, dg not set.")
  129. }
  130. _, err := dg.VoiceRegions()
  131. if err != nil {
  132. t.Errorf("VoiceRegions() returned error: %+v", err)
  133. }
  134. }
  135. func TestGuildRoles(t *testing.T) {
  136. if envGuild == "" {
  137. t.Skip("Skipping, DG_GUILD not set.")
  138. }
  139. if dg == nil {
  140. t.Skip("Skipping, dg not set.")
  141. }
  142. _, err := dg.GuildRoles(envGuild)
  143. if err != nil {
  144. t.Errorf("GuildRoles(envGuild) returned error: %+v", err)
  145. }
  146. }
  147. // TestChannelMessageSend2 tests the ChannelMessageSend() function. This should not return an error.
  148. func TestChannelMessageSend2(t *testing.T) {
  149. if envChannel == "" {
  150. t.Skip("Skipping, DG_CHANNEL not set.")
  151. }
  152. if dg == nil {
  153. t.Skip("Skipping, dg not set.")
  154. }
  155. _, err := dg.ChannelMessageSend(envChannel, "All done running REST API Tests!")
  156. if err != nil {
  157. t.Errorf("ChannelMessageSend returned error: %+v", err)
  158. }
  159. }