restapi_test.go 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. /*
  21. // removed for now, only works on BOT accounts now
  22. func TestUserAvatar(t *testing.T) {
  23. if dg == nil {
  24. t.Skip("Cannot TestUserAvatar, dg not set.")
  25. }
  26. u, err := dg.User("@me")
  27. if err != nil {
  28. t.Error("error fetching @me user,", err)
  29. }
  30. a, err := dg.UserAvatar(u.ID)
  31. if err != nil {
  32. if err.Error() == `HTTP 404 NOT FOUND, {"code": 0, "message": "404: Not Found"}` {
  33. t.Skip("Skipped, @me doesn't have an Avatar")
  34. }
  35. t.Errorf(err.Error())
  36. }
  37. if a == nil {
  38. t.Errorf("a == nil, should be image.Image")
  39. }
  40. }
  41. */
  42. /* Running this causes an error due to 2/hour rate limit on username changes
  43. func TestUserUpdate(t *testing.T) {
  44. if dg == nil {
  45. t.Skip("Cannot test logout, dg not set.")
  46. }
  47. u, err := dg.User("@me")
  48. if err != nil {
  49. t.Errorf(err.Error())
  50. }
  51. s, err := dg.UserUpdate(envEmail, envPassword, "testname", u.Avatar, "")
  52. if err != nil {
  53. t.Error(err.Error())
  54. }
  55. if s.Username != "testname" {
  56. t.Error("Username != testname")
  57. }
  58. s, err = dg.UserUpdate(envEmail, envPassword, u.Username, u.Avatar, "")
  59. if err != nil {
  60. t.Error(err.Error())
  61. }
  62. if s.Username != u.Username {
  63. t.Error("Username != " + u.Username)
  64. }
  65. }
  66. */
  67. //func (s *Session) UserChannelCreate(recipientID string) (st *Channel, err error) {
  68. func TestUserChannelCreate(t *testing.T) {
  69. if dg == nil {
  70. t.Skip("Cannot TestUserChannelCreate, dg not set.")
  71. }
  72. if envAdmin == "" {
  73. t.Skip("Skipped, DG_ADMIN not set.")
  74. }
  75. _, err := dg.UserChannelCreate(envAdmin)
  76. if err != nil {
  77. t.Errorf(err.Error())
  78. }
  79. // TODO make sure the channel was added
  80. }
  81. func TestUserChannels(t *testing.T) {
  82. if dg == nil {
  83. t.Skip("Cannot TestUserChannels, dg not set.")
  84. }
  85. _, err := dg.UserChannels()
  86. if err != nil {
  87. t.Errorf(err.Error())
  88. }
  89. }
  90. func TestUserGuilds(t *testing.T) {
  91. if dg == nil {
  92. t.Skip("Cannot TestUserGuilds, dg not set.")
  93. }
  94. _, err := dg.UserGuilds(10, "", "")
  95. if err != nil {
  96. t.Errorf(err.Error())
  97. }
  98. }
  99. func TestUserSettings(t *testing.T) {
  100. if dg == nil {
  101. t.Skip("Cannot TestUserSettings, dg not set.")
  102. }
  103. _, err := dg.UserSettings()
  104. if err != nil {
  105. t.Errorf(err.Error())
  106. }
  107. }
  108. func TestUserUpdateStatus(t *testing.T) {
  109. if dg == nil {
  110. t.Skip("Cannot TestUserSettings, dg not set.")
  111. }
  112. _, err := dg.UserUpdateStatus(StatusDoNotDisturb)
  113. if err != nil {
  114. t.Errorf(err.Error())
  115. }
  116. }
  117. // TestLogout tests the Logout() function. This should not return an error.
  118. func TestLogout(t *testing.T) {
  119. if dg == nil {
  120. t.Skip("Cannot TestLogout, dg not set.")
  121. }
  122. err := dg.Logout()
  123. if err != nil {
  124. t.Errorf("Logout() returned error: %+v", err)
  125. }
  126. }
  127. func TestGateway(t *testing.T) {
  128. if dg == nil {
  129. t.Skip("Skipping, dg not set.")
  130. }
  131. _, err := dg.Gateway()
  132. if err != nil {
  133. t.Errorf("Gateway() returned error: %+v", err)
  134. }
  135. }
  136. func TestGatewayBot(t *testing.T) {
  137. if dgBot == nil {
  138. t.Skip("Skipping, dgBot not set.")
  139. }
  140. _, err := dgBot.GatewayBot()
  141. if err != nil {
  142. t.Errorf("GatewayBot() returned error: %+v", err)
  143. }
  144. }
  145. func TestVoiceICE(t *testing.T) {
  146. if dg == nil {
  147. t.Skip("Skipping, dg not set.")
  148. }
  149. _, err := dg.VoiceICE()
  150. if err != nil {
  151. t.Errorf("VoiceICE() returned error: %+v", err)
  152. }
  153. }
  154. func TestVoiceRegions(t *testing.T) {
  155. if dg == nil {
  156. t.Skip("Skipping, dg not set.")
  157. }
  158. _, err := dg.VoiceRegions()
  159. if err != nil {
  160. t.Errorf("VoiceRegions() returned error: %+v", err)
  161. }
  162. }
  163. func TestGuildRoles(t *testing.T) {
  164. if envGuild == "" {
  165. t.Skip("Skipping, DG_GUILD not set.")
  166. }
  167. if dg == nil {
  168. t.Skip("Skipping, dg not set.")
  169. }
  170. _, err := dg.GuildRoles(envGuild)
  171. if err != nil {
  172. t.Errorf("GuildRoles(envGuild) returned error: %+v", err)
  173. }
  174. }
  175. func TestGuildMemberNickname(t *testing.T) {
  176. if envGuild == "" {
  177. t.Skip("Skipping, DG_GUILD not set.")
  178. }
  179. if dg == nil {
  180. t.Skip("Skipping, dg not set.")
  181. }
  182. err := dg.GuildMemberNickname(envGuild, "@me/nick", "B1nzyRocks")
  183. if err != nil {
  184. t.Errorf("GuildNickname returned error: %+v", err)
  185. }
  186. }
  187. // TestChannelMessageSend2 tests the ChannelMessageSend() function. This should not return an error.
  188. func TestChannelMessageSend2(t *testing.T) {
  189. if envChannel == "" {
  190. t.Skip("Skipping, DG_CHANNEL not set.")
  191. }
  192. if dg == nil {
  193. t.Skip("Skipping, dg not set.")
  194. }
  195. _, err := dg.ChannelMessageSend(envChannel, "All done running REST API Tests!")
  196. if err != nil {
  197. t.Errorf("ChannelMessageSend returned error: %+v", err)
  198. }
  199. }
  200. // TestGuildPruneCount tests GuildPruneCount() function. This should not return an error.
  201. func TestGuildPruneCount(t *testing.T) {
  202. if envGuild == "" {
  203. t.Skip("Skipping, DG_GUILD not set.")
  204. }
  205. if dg == nil {
  206. t.Skip("Skipping, dg not set.")
  207. }
  208. _, err := dg.GuildPruneCount(envGuild, 1)
  209. if err != nil {
  210. t.Errorf("GuildPruneCount returned error: %+v", err)
  211. }
  212. }
  213. /*
  214. // TestGuildPrune tests GuildPrune() function. This should not return an error.
  215. func TestGuildPrune(t *testing.T) {
  216. if envGuild == "" {
  217. t.Skip("Skipping, DG_GUILD not set.")
  218. }
  219. if dg == nil {
  220. t.Skip("Skipping, dg not set.")
  221. }
  222. _, err := dg.GuildPrune(envGuild, 1)
  223. if err != nil {
  224. t.Errorf("GuildPrune returned error: %+v", err)
  225. }
  226. }
  227. */