restapi_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. package discordgo
  2. import (
  3. "errors"
  4. "testing"
  5. )
  6. //////////////////////////////////////////////////////////////////////////////
  7. /////////////////////////////////////////////////////////////// START OF TESTS
  8. // TestChannelMessageSend tests the ChannelMessageSend() function. This should not return an error.
  9. func TestChannelMessageSend(t *testing.T) {
  10. if envChannel == "" {
  11. t.Skip("Skipping, DG_CHANNEL not set.")
  12. }
  13. if dg == nil {
  14. t.Skip("Skipping, dg not set.")
  15. }
  16. _, err := dg.ChannelMessageSend(envChannel, "Running REST API Tests!")
  17. if err != nil {
  18. t.Errorf("ChannelMessageSend returned error: %+v", err)
  19. }
  20. }
  21. /*
  22. // removed for now, only works on BOT accounts now
  23. func TestUserAvatar(t *testing.T) {
  24. if dg == nil {
  25. t.Skip("Cannot TestUserAvatar, dg not set.")
  26. }
  27. u, err := dg.User("@me")
  28. if err != nil {
  29. t.Error("error fetching @me user,", err)
  30. }
  31. a, err := dg.UserAvatar(u.ID)
  32. if err != nil {
  33. if err.Error() == `HTTP 404 NOT FOUND, {"code": 0, "message": "404: Not Found"}` {
  34. t.Skip("Skipped, @me doesn't have an Avatar")
  35. }
  36. t.Errorf(err.Error())
  37. }
  38. if a == nil {
  39. t.Errorf("a == nil, should be image.Image")
  40. }
  41. }
  42. */
  43. /* Running this causes an error due to 2/hour rate limit on username changes
  44. func TestUserUpdate(t *testing.T) {
  45. if dg == nil {
  46. t.Skip("Cannot test logout, dg not set.")
  47. }
  48. u, err := dg.User("@me")
  49. if err != nil {
  50. t.Errorf(err.Error())
  51. }
  52. s, err := dg.UserUpdate(envEmail, envPassword, "testname", u.Avatar, "")
  53. if err != nil {
  54. t.Error(err.Error())
  55. }
  56. if s.Username != "testname" {
  57. t.Error("Username != testname")
  58. }
  59. s, err = dg.UserUpdate(envEmail, envPassword, u.Username, u.Avatar, "")
  60. if err != nil {
  61. t.Error(err.Error())
  62. }
  63. if s.Username != u.Username {
  64. t.Error("Username != " + u.Username)
  65. }
  66. }
  67. */
  68. //func (s *Session) UserChannelCreate(recipientID string) (st *Channel, err error) {
  69. func TestUserChannelCreate(t *testing.T) {
  70. if dg == nil {
  71. t.Skip("Cannot TestUserChannelCreate, dg not set.")
  72. }
  73. if envAdmin == "" {
  74. t.Skip("Skipped, DG_ADMIN not set.")
  75. }
  76. _, err := dg.UserChannelCreate(envAdmin)
  77. if err != nil {
  78. t.Errorf(err.Error())
  79. }
  80. // TODO make sure the channel was added
  81. }
  82. func TestUserGuilds(t *testing.T) {
  83. if dg == nil {
  84. t.Skip("Cannot TestUserGuilds, dg not set.")
  85. }
  86. _, err := dg.UserGuilds(10, "", "")
  87. if err != nil {
  88. t.Errorf(err.Error())
  89. }
  90. }
  91. func TestGateway(t *testing.T) {
  92. if dg == nil {
  93. t.Skip("Skipping, dg not set.")
  94. }
  95. _, err := dg.Gateway()
  96. if err != nil {
  97. t.Errorf("Gateway() returned error: %+v", err)
  98. }
  99. }
  100. func TestGatewayBot(t *testing.T) {
  101. if dgBot == nil {
  102. t.Skip("Skipping, dgBot not set.")
  103. }
  104. _, err := dgBot.GatewayBot()
  105. if err != nil {
  106. t.Errorf("GatewayBot() returned error: %+v", err)
  107. }
  108. }
  109. func TestVoiceRegions(t *testing.T) {
  110. if dg == nil {
  111. t.Skip("Skipping, dg not set.")
  112. }
  113. _, err := dg.VoiceRegions()
  114. if err != nil {
  115. t.Errorf("VoiceRegions() returned error: %+v", err)
  116. }
  117. }
  118. func TestGuildRoles(t *testing.T) {
  119. if envGuild == "" {
  120. t.Skip("Skipping, DG_GUILD not set.")
  121. }
  122. if dg == nil {
  123. t.Skip("Skipping, dg not set.")
  124. }
  125. _, err := dg.GuildRoles(envGuild)
  126. if err != nil {
  127. t.Errorf("GuildRoles(envGuild) returned error: %+v", err)
  128. }
  129. }
  130. func TestGuildMemberNickname(t *testing.T) {
  131. if envGuild == "" {
  132. t.Skip("Skipping, DG_GUILD not set.")
  133. }
  134. if dg == nil {
  135. t.Skip("Skipping, dg not set.")
  136. }
  137. err := dg.GuildMemberNickname(envGuild, "@me/nick", "B1nzyRocks")
  138. if err != nil {
  139. t.Errorf("GuildNickname returned error: %+v", err)
  140. }
  141. }
  142. // TestChannelMessageSend2 tests the ChannelMessageSend() function. This should not return an error.
  143. func TestChannelMessageSend2(t *testing.T) {
  144. if envChannel == "" {
  145. t.Skip("Skipping, DG_CHANNEL not set.")
  146. }
  147. if dg == nil {
  148. t.Skip("Skipping, dg not set.")
  149. }
  150. _, err := dg.ChannelMessageSend(envChannel, "All done running REST API Tests!")
  151. if err != nil {
  152. t.Errorf("ChannelMessageSend returned error: %+v", err)
  153. }
  154. }
  155. // TestGuildPruneCount tests GuildPruneCount() function. This should not return an error.
  156. func TestGuildPruneCount(t *testing.T) {
  157. if envGuild == "" {
  158. t.Skip("Skipping, DG_GUILD not set.")
  159. }
  160. if dg == nil {
  161. t.Skip("Skipping, dg not set.")
  162. }
  163. _, err := dg.GuildPruneCount(envGuild, 1)
  164. if err != nil {
  165. t.Errorf("GuildPruneCount returned error: %+v", err)
  166. }
  167. }
  168. /*
  169. // TestGuildPrune tests GuildPrune() function. This should not return an error.
  170. func TestGuildPrune(t *testing.T) {
  171. if envGuild == "" {
  172. t.Skip("Skipping, DG_GUILD not set.")
  173. }
  174. if dg == nil {
  175. t.Skip("Skipping, dg not set.")
  176. }
  177. _, err := dg.GuildPrune(envGuild, 1)
  178. if err != nil {
  179. t.Errorf("GuildPrune returned error: %+v", err)
  180. }
  181. }
  182. */
  183. func Test_unmarshal(t *testing.T) {
  184. err := unmarshal([]byte{}, &struct{}{})
  185. if !errors.Is(err, ErrJSONUnmarshal) {
  186. t.Errorf("Unexpected error type: %T", err)
  187. }
  188. }