restapi_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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 TestUserChannels(t *testing.T) {
  83. if dg == nil {
  84. t.Skip("Cannot TestUserChannels, dg not set.")
  85. }
  86. _, err := dg.UserChannels()
  87. if err != nil {
  88. t.Errorf(err.Error())
  89. }
  90. }
  91. func TestUserGuilds(t *testing.T) {
  92. if dg == nil {
  93. t.Skip("Cannot TestUserGuilds, dg not set.")
  94. }
  95. _, err := dg.UserGuilds(10, "", "")
  96. if err != nil {
  97. t.Errorf(err.Error())
  98. }
  99. }
  100. func TestUserSettings(t *testing.T) {
  101. if dg == nil {
  102. t.Skip("Cannot TestUserSettings, dg not set.")
  103. }
  104. _, err := dg.UserSettings()
  105. if err != nil {
  106. t.Errorf(err.Error())
  107. }
  108. }
  109. func TestUserUpdateStatus(t *testing.T) {
  110. if dg == nil {
  111. t.Skip("Cannot TestUserSettings, dg not set.")
  112. }
  113. _, err := dg.UserUpdateStatus(StatusDoNotDisturb)
  114. if err != nil {
  115. t.Errorf(err.Error())
  116. }
  117. }
  118. // TestLogout tests the Logout() function. This should not return an error.
  119. func TestLogout(t *testing.T) {
  120. if dg == nil {
  121. t.Skip("Cannot TestLogout, dg not set.")
  122. }
  123. err := dg.Logout()
  124. if err != nil {
  125. t.Errorf("Logout() returned error: %+v", err)
  126. }
  127. }
  128. func TestGateway(t *testing.T) {
  129. if dg == nil {
  130. t.Skip("Skipping, dg not set.")
  131. }
  132. _, err := dg.Gateway()
  133. if err != nil {
  134. t.Errorf("Gateway() returned error: %+v", err)
  135. }
  136. }
  137. func TestGatewayBot(t *testing.T) {
  138. if dgBot == nil {
  139. t.Skip("Skipping, dgBot not set.")
  140. }
  141. _, err := dgBot.GatewayBot()
  142. if err != nil {
  143. t.Errorf("GatewayBot() returned error: %+v", err)
  144. }
  145. }
  146. func TestVoiceICE(t *testing.T) {
  147. if dg == nil {
  148. t.Skip("Skipping, dg not set.")
  149. }
  150. _, err := dg.VoiceICE()
  151. if err != nil {
  152. t.Errorf("VoiceICE() returned error: %+v", err)
  153. }
  154. }
  155. func TestVoiceRegions(t *testing.T) {
  156. if dg == nil {
  157. t.Skip("Skipping, dg not set.")
  158. }
  159. _, err := dg.VoiceRegions()
  160. if err != nil {
  161. t.Errorf("VoiceRegions() returned error: %+v", err)
  162. }
  163. }
  164. func TestGuildRoles(t *testing.T) {
  165. if envGuild == "" {
  166. t.Skip("Skipping, DG_GUILD not set.")
  167. }
  168. if dg == nil {
  169. t.Skip("Skipping, dg not set.")
  170. }
  171. _, err := dg.GuildRoles(envGuild)
  172. if err != nil {
  173. t.Errorf("GuildRoles(envGuild) returned error: %+v", err)
  174. }
  175. }
  176. func TestGuildMemberNickname(t *testing.T) {
  177. if envGuild == "" {
  178. t.Skip("Skipping, DG_GUILD not set.")
  179. }
  180. if dg == nil {
  181. t.Skip("Skipping, dg not set.")
  182. }
  183. err := dg.GuildMemberNickname(envGuild, "@me/nick", "B1nzyRocks")
  184. if err != nil {
  185. t.Errorf("GuildNickname returned error: %+v", err)
  186. }
  187. }
  188. // TestChannelMessageSend2 tests the ChannelMessageSend() function. This should not return an error.
  189. func TestChannelMessageSend2(t *testing.T) {
  190. if envChannel == "" {
  191. t.Skip("Skipping, DG_CHANNEL not set.")
  192. }
  193. if dg == nil {
  194. t.Skip("Skipping, dg not set.")
  195. }
  196. _, err := dg.ChannelMessageSend(envChannel, "All done running REST API Tests!")
  197. if err != nil {
  198. t.Errorf("ChannelMessageSend returned error: %+v", err)
  199. }
  200. }
  201. // TestGuildPruneCount tests GuildPruneCount() function. This should not return an error.
  202. func TestGuildPruneCount(t *testing.T) {
  203. if envGuild == "" {
  204. t.Skip("Skipping, DG_GUILD not set.")
  205. }
  206. if dg == nil {
  207. t.Skip("Skipping, dg not set.")
  208. }
  209. _, err := dg.GuildPruneCount(envGuild, 1)
  210. if err != nil {
  211. t.Errorf("GuildPruneCount returned error: %+v", err)
  212. }
  213. }
  214. /*
  215. // TestGuildPrune tests GuildPrune() function. This should not return an error.
  216. func TestGuildPrune(t *testing.T) {
  217. if envGuild == "" {
  218. t.Skip("Skipping, DG_GUILD not set.")
  219. }
  220. if dg == nil {
  221. t.Skip("Skipping, dg not set.")
  222. }
  223. _, err := dg.GuildPrune(envGuild, 1)
  224. if err != nil {
  225. t.Errorf("GuildPrune returned error: %+v", err)
  226. }
  227. }
  228. */
  229. func Test_unmarshal(t *testing.T) {
  230. err := unmarshal([]byte{}, &struct{}{})
  231. if !errors.Is(err, ErrJSONUnmarshal) {
  232. t.Errorf("Unexpected error type: %T", err)
  233. }
  234. }