附近小店
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

301 lines
6.1 KiB

  1. package weapp
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "testing"
  7. )
  8. func TestApplyPlugin(t *testing.T) {
  9. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  10. if r.Method != "POST" {
  11. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  12. }
  13. path := r.URL.EscapedPath()
  14. if path != apiPlugin {
  15. t.Fatalf("Except to path '%s',get '%s'", apiPlugin, path)
  16. }
  17. if err := r.ParseForm(); err != nil {
  18. t.Fatal(err)
  19. }
  20. if r.Form.Get("access_token") == "" {
  21. t.Fatalf("access_token can not be empty")
  22. }
  23. params := struct {
  24. Action string `json:"action"`
  25. PluginAppID string `json:"plugin_appid"`
  26. Reason string `json:"reason"`
  27. }{}
  28. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  29. t.Fatal(err)
  30. }
  31. if params.Action != "apply" {
  32. t.Error("Unexpected action")
  33. }
  34. if params.PluginAppID == "" {
  35. t.Error("Response column plugin_appid can not be empty")
  36. }
  37. w.WriteHeader(http.StatusOK)
  38. raw := `{
  39. "errcode": 0,
  40. "errmsg": "ok"
  41. }`
  42. if _, err := w.Write([]byte(raw)); err != nil {
  43. t.Fatal(err)
  44. }
  45. }))
  46. defer ts.Close()
  47. _, err := applyPlugin(ts.URL+apiPlugin, "mock-access-token", "plugin-app-id", "mock-reason")
  48. if err != nil {
  49. t.Fatal(err)
  50. }
  51. }
  52. func TestGetPluginDevApplyList(t *testing.T) {
  53. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  54. if r.Method != "POST" {
  55. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  56. }
  57. path := r.URL.EscapedPath()
  58. if path != apiDevPlugin {
  59. t.Fatalf("Except to path '%s',get '%s'", apiDevPlugin, path)
  60. }
  61. if err := r.ParseForm(); err != nil {
  62. t.Fatal(err)
  63. }
  64. if r.Form.Get("access_token") == "" {
  65. t.Fatalf("access_token can not be empty")
  66. }
  67. params := struct {
  68. Action string `json:"action"`
  69. Page uint `json:"page"`
  70. Number uint `json:"num"`
  71. }{}
  72. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  73. t.Fatal(err)
  74. }
  75. if params.Action != "dev_apply_list" {
  76. t.Error("Unexpected action")
  77. }
  78. if params.Page == 0 {
  79. t.Error("Response column page can not be zero")
  80. }
  81. if params.Number == 0 {
  82. t.Error("Response column num can not be zero")
  83. }
  84. w.WriteHeader(http.StatusOK)
  85. raw := `{
  86. "errcode": 0,
  87. "errmsg": "ok",
  88. "apply_list": [{
  89. "appid": "xxxxxxxxxxxxx",
  90. "status": 1,
  91. "nickname": "名称",
  92. "headimgurl": "**********",
  93. "reason": "polo has gone",
  94. "apply_url": "*******",
  95. "create_time": "1536305096",
  96. "categories": [{
  97. "first": "IT科技",
  98. "second": "硬件与设备"
  99. }]
  100. }]
  101. }`
  102. if _, err := w.Write([]byte(raw)); err != nil {
  103. t.Fatal(err)
  104. }
  105. }))
  106. defer ts.Close()
  107. _, err := getPluginDevApplyList(ts.URL+apiDevPlugin, "mock-access-token", 1, 2)
  108. if err != nil {
  109. t.Fatal(err)
  110. }
  111. }
  112. func TestGetPluginList(t *testing.T) {
  113. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  114. if r.Method != "POST" {
  115. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  116. }
  117. path := r.URL.EscapedPath()
  118. if path != apiPlugin {
  119. t.Fatalf("Except to path '%s',get '%s'", apiPlugin, path)
  120. }
  121. if err := r.ParseForm(); err != nil {
  122. t.Fatal(err)
  123. }
  124. if r.Form.Get("access_token") == "" {
  125. t.Fatalf("access_token can not be empty")
  126. }
  127. params := struct {
  128. Action string `json:"action"`
  129. }{}
  130. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  131. t.Fatal(err)
  132. }
  133. if params.Action != "list" {
  134. t.Error("Unexpected action")
  135. }
  136. w.WriteHeader(http.StatusOK)
  137. raw := `{
  138. "errcode": 0,
  139. "errmsg": "ok",
  140. "plugin_list": [{
  141. "appid": "aaaa",
  142. "status": 1,
  143. "nickname": "插件昵称",
  144. "headimgurl": "http://plugin.qq.com"
  145. }]
  146. }`
  147. if _, err := w.Write([]byte(raw)); err != nil {
  148. t.Fatal(err)
  149. }
  150. }))
  151. defer ts.Close()
  152. _, err := getPluginList(ts.URL+apiPlugin, "mock-access-token")
  153. if err != nil {
  154. t.Fatal(err)
  155. }
  156. }
  157. func TestSetDevPluginApplyStatus(t *testing.T) {
  158. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  159. if r.Method != "POST" {
  160. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  161. }
  162. path := r.URL.EscapedPath()
  163. if path != apiDevPlugin {
  164. t.Fatalf("Except to path '%s',get '%s'", apiDevPlugin, path)
  165. }
  166. if err := r.ParseForm(); err != nil {
  167. t.Fatal(err)
  168. }
  169. if r.Form.Get("access_token") == "" {
  170. t.Fatalf("access_token can not be empty")
  171. }
  172. params := struct {
  173. Action string `json:"action"`
  174. AppID string `json:"appid"`
  175. Reason string `json:"reason"`
  176. }{}
  177. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  178. t.Fatal(err)
  179. }
  180. if params.Action == "" {
  181. t.Error("Response column action can not be empty")
  182. }
  183. w.WriteHeader(http.StatusOK)
  184. raw := `{
  185. "errcode": 0,
  186. "errmsg": "ok"
  187. }`
  188. if _, err := w.Write([]byte(raw)); err != nil {
  189. t.Fatal(err)
  190. }
  191. }))
  192. defer ts.Close()
  193. _, err := setDevPluginApplyStatus(ts.URL+apiDevPlugin, "mock-access-token", "mock-plugin-app-id", "mock-reason", DevAgree)
  194. if err != nil {
  195. t.Fatal(err)
  196. }
  197. }
  198. func TestUnbindPlugin(t *testing.T) {
  199. ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
  200. if r.Method != "POST" {
  201. t.Fatalf("Expect 'POST' get '%s'", r.Method)
  202. }
  203. path := r.URL.EscapedPath()
  204. if path != apiPlugin {
  205. t.Fatalf("Except to path '%s',get '%s'", apiPlugin, path)
  206. }
  207. if err := r.ParseForm(); err != nil {
  208. t.Fatal(err)
  209. }
  210. if r.Form.Get("access_token") == "" {
  211. t.Fatalf("access_token can not be empty")
  212. }
  213. params := struct {
  214. Action string `json:"action"`
  215. PluginAppID string `json:"plugin_appid"`
  216. }{}
  217. if err := json.NewDecoder(r.Body).Decode(&params); err != nil {
  218. t.Fatal(err)
  219. }
  220. if params.Action != "unbind" {
  221. t.Error("Unexpected action")
  222. }
  223. if params.PluginAppID == "" {
  224. t.Error("Response column plugin_appid can not be empty")
  225. }
  226. w.WriteHeader(http.StatusOK)
  227. raw := `{
  228. "errcode": 0,
  229. "errmsg": "ok"
  230. }`
  231. if _, err := w.Write([]byte(raw)); err != nil {
  232. t.Fatal(err)
  233. }
  234. }))
  235. defer ts.Close()
  236. _, err := unbindPlugin(ts.URL+apiPlugin, "mock-access-token", "mock-plugin-app-id")
  237. if err != nil {
  238. t.Fatal(err)
  239. }
  240. }