user_handler_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739
  1. package api_test
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "net/http/httptest"
  7. "reflect"
  8. "strings"
  9. "testing"
  10. "github.com/porter-dev/porter/internal/models"
  11. )
  12. // ------------------------- TEST TYPES AND MAIN LOOP ------------------------- //
  13. type userTest struct {
  14. initializers []func(t *tester)
  15. msg string
  16. method string
  17. endpoint string
  18. body string
  19. expStatus int
  20. expBody string
  21. useCookie bool
  22. validators []func(c *userTest, tester *tester, t *testing.T)
  23. }
  24. func testUserRequests(t *testing.T, tests []*userTest, canQuery bool) {
  25. for _, c := range tests {
  26. // create a new tester
  27. tester := newTester(canQuery)
  28. // if there's an initializer, call it
  29. for _, init := range c.initializers {
  30. init(tester)
  31. }
  32. req, err := http.NewRequest(
  33. c.method,
  34. c.endpoint,
  35. strings.NewReader(c.body),
  36. )
  37. tester.req = req
  38. if c.useCookie {
  39. req.AddCookie(tester.cookie)
  40. }
  41. if err != nil {
  42. t.Fatal(err)
  43. }
  44. tester.execute()
  45. rr := tester.rr
  46. // first, check that the status matches
  47. if status := rr.Code; status != c.expStatus {
  48. t.Errorf("%s, handler returned wrong status code: got %v want %v",
  49. c.msg, status, c.expStatus)
  50. }
  51. // if there's a validator, call it
  52. for _, validate := range c.validators {
  53. validate(c, tester, t)
  54. }
  55. }
  56. }
  57. // ------------------------- TEST FIXTURES AND FUNCTIONS ------------------------- //
  58. var authCheckTests = []*userTest{
  59. &userTest{
  60. initializers: []func(tester *tester){
  61. initUserDefault,
  62. },
  63. msg: "Auth check successful. User is logged in.",
  64. method: "GET",
  65. endpoint: "/api/auth/check",
  66. expStatus: http.StatusOK,
  67. body: "",
  68. expBody: `{"id":1,"email":"","contexts":null,"rawKubeConfig":""}`,
  69. useCookie: true,
  70. validators: []func(c *userTest, tester *tester, t *testing.T){
  71. userBasicBodyValidator,
  72. },
  73. },
  74. &userTest{
  75. initializers: []func(tester *tester){
  76. initUserDefault,
  77. },
  78. msg: "Auth check failure. User is not logged in.",
  79. method: "GET",
  80. endpoint: "/api/auth/check",
  81. body: "",
  82. expStatus: http.StatusForbidden,
  83. expBody: http.StatusText(http.StatusForbidden) + "\n",
  84. validators: []func(c *userTest, tester *tester, t *testing.T){
  85. userBasicBodyValidator,
  86. },
  87. },
  88. }
  89. func TestHandleAuthCheck(t *testing.T) {
  90. testUserRequests(t, authCheckTests, true)
  91. }
  92. var createUserTests = []*userTest{
  93. &userTest{
  94. msg: "Create user",
  95. method: "POST",
  96. endpoint: "/api/users",
  97. body: `{
  98. "email": "belanger@getporter.dev",
  99. "password": "hello"
  100. }`,
  101. expStatus: http.StatusCreated,
  102. expBody: `{"id":1,"email":"","contexts":null,"rawKubeConfig":""}`,
  103. },
  104. &userTest{
  105. msg: "Create user invalid email",
  106. method: "POST",
  107. endpoint: "/api/users",
  108. body: `{
  109. "email": "notanemail",
  110. "password": "hello"
  111. }`,
  112. expStatus: http.StatusUnprocessableEntity,
  113. expBody: `{"code":601,"errors":["email validation failed"]}`,
  114. validators: []func(c *userTest, tester *tester, t *testing.T){
  115. userBasicBodyValidator,
  116. },
  117. },
  118. &userTest{
  119. msg: "Create user missing field",
  120. method: "POST",
  121. endpoint: "/api/users",
  122. body: `{
  123. "password": "hello"
  124. }`,
  125. expStatus: http.StatusUnprocessableEntity,
  126. expBody: `{"code":601,"errors":["required validation failed"]}`,
  127. validators: []func(c *userTest, tester *tester, t *testing.T){
  128. userBasicBodyValidator,
  129. },
  130. },
  131. &userTest{
  132. initializers: []func(tester *tester){
  133. initUserDefault,
  134. },
  135. msg: "Create user same email",
  136. method: "POST",
  137. endpoint: "/api/users",
  138. body: `{
  139. "email": "belanger@getporter.dev",
  140. "password": "hello"
  141. }`,
  142. expStatus: http.StatusUnprocessableEntity,
  143. expBody: `{"code":601,"errors":["email already taken"]}`,
  144. validators: []func(c *userTest, tester *tester, t *testing.T){
  145. userBasicBodyValidator,
  146. },
  147. },
  148. &userTest{
  149. msg: "Create user invalid field type",
  150. method: "POST",
  151. endpoint: "/api/users",
  152. body: `{
  153. "email": "belanger@getporter.dev",
  154. "password": 0
  155. }`,
  156. expStatus: http.StatusBadRequest,
  157. expBody: `{"code":600,"errors":["could not process request"]}`,
  158. validators: []func(c *userTest, tester *tester, t *testing.T){
  159. userBasicBodyValidator,
  160. },
  161. },
  162. }
  163. func TestHandleCreateUser(t *testing.T) {
  164. testUserRequests(t, createUserTests, true)
  165. }
  166. var createUserTestsWriteFail = []*userTest{
  167. &userTest{
  168. msg: "Create user db connection down",
  169. method: "POST",
  170. endpoint: "/api/users",
  171. body: `{
  172. "email": "belanger@getporter.dev",
  173. "password": "hello"
  174. }`,
  175. expStatus: http.StatusInternalServerError,
  176. expBody: `{"code":500,"errors":["could not read from database"]}`,
  177. validators: []func(c *userTest, tester *tester, t *testing.T){
  178. userBasicBodyValidator,
  179. },
  180. },
  181. }
  182. func TestHandleCreateUserWriteFail(t *testing.T) {
  183. testUserRequests(t, createUserTestsWriteFail, false)
  184. }
  185. var loginUserTests = []*userTest{
  186. &userTest{
  187. initializers: []func(tester *tester){
  188. initUserDefault,
  189. },
  190. msg: "Login user successful",
  191. method: "POST",
  192. endpoint: "/api/login",
  193. body: `{
  194. "email": "belanger@getporter.dev",
  195. "password": "hello"
  196. }`,
  197. expStatus: http.StatusOK,
  198. expBody: `{"id":1,"email":"","contexts":null,"rawKubeConfig":""}`,
  199. validators: []func(c *userTest, tester *tester, t *testing.T){
  200. userBasicBodyValidator,
  201. },
  202. },
  203. &userTest{
  204. initializers: []func(tester *tester){
  205. initUserDefault,
  206. },
  207. msg: "Login user already logged in",
  208. method: "POST",
  209. endpoint: "/api/login",
  210. body: `{
  211. "email": "belanger@getporter.dev",
  212. "password": "hello"
  213. }`,
  214. expStatus: http.StatusOK,
  215. expBody: `{"id":1,"email":"","contexts":null,"rawKubeConfig":""}`,
  216. useCookie: true,
  217. validators: []func(c *userTest, tester *tester, t *testing.T){
  218. userBasicBodyValidator,
  219. },
  220. },
  221. &userTest{
  222. msg: "Login user unregistered email",
  223. method: "POST",
  224. endpoint: "/api/login",
  225. body: `{
  226. "email": "belanger@getporter.dev",
  227. "password": "hello"
  228. }`,
  229. expStatus: http.StatusUnauthorized,
  230. expBody: `{"code":401,"errors":["email not registered"]}`,
  231. validators: []func(c *userTest, tester *tester, t *testing.T){
  232. userBasicBodyValidator,
  233. },
  234. },
  235. &userTest{
  236. initializers: []func(tester *tester){
  237. initUserDefault,
  238. },
  239. msg: "Login user incorrect password",
  240. method: "POST",
  241. endpoint: "/api/login",
  242. body: `{
  243. "email": "belanger@getporter.dev",
  244. "password": "notthepassword"
  245. }`,
  246. expStatus: http.StatusUnauthorized,
  247. expBody: `{"code":401,"errors":["incorrect password"]}`,
  248. useCookie: true,
  249. validators: []func(c *userTest, tester *tester, t *testing.T){
  250. userBasicBodyValidator,
  251. },
  252. },
  253. }
  254. func TestHandleLoginUser(t *testing.T) {
  255. testUserRequests(t, loginUserTests, true)
  256. }
  257. var logoutUserTests = []*userTest{
  258. &userTest{
  259. initializers: []func(tester *tester){
  260. initUserDefault,
  261. },
  262. msg: "Logout user successful",
  263. method: "POST",
  264. endpoint: "/api/logout",
  265. body: `{
  266. "email": "belanger@getporter.dev",
  267. "password": "hello"
  268. }`,
  269. expStatus: http.StatusOK,
  270. expBody: ``,
  271. useCookie: true,
  272. validators: []func(c *userTest, tester *tester, t *testing.T){
  273. func(c *userTest, tester *tester, t *testing.T) {
  274. req, err := http.NewRequest(
  275. "GET",
  276. "/api/users/1",
  277. strings.NewReader(""),
  278. )
  279. req.AddCookie(tester.cookie)
  280. if err != nil {
  281. t.Fatal(err)
  282. }
  283. rr2 := httptest.NewRecorder()
  284. tester.router.ServeHTTP(rr2, req)
  285. if status := rr2.Code; status != http.StatusForbidden {
  286. t.Errorf("%s, handler returned wrong status: got %v want %v",
  287. "validator failed", status, http.StatusForbidden)
  288. }
  289. },
  290. },
  291. },
  292. }
  293. func TestHandleLogoutUser(t *testing.T) {
  294. testUserRequests(t, logoutUserTests, true)
  295. }
  296. var readUserTests = []*userTest{
  297. &userTest{
  298. initializers: []func(tester *tester){
  299. initUserWithContexts,
  300. },
  301. msg: "Read user successful",
  302. method: "GET",
  303. endpoint: "/api/users/1",
  304. body: "",
  305. expStatus: http.StatusOK,
  306. expBody: `{"id":1,"email":"belanger@getporter.dev","contexts":["context-test"],"rawKubeConfig":"apiVersion: v1\nkind: Config\npreferences: {}\ncurrent-context: context-test\nclusters:\n- cluster:\n server: https://localhost\n name: cluster-test\ncontexts:\n- context:\n cluster: cluster-test\n user: test-admin\n name: context-test\nusers:\n- name: test-admin"}`,
  307. useCookie: true,
  308. validators: []func(c *userTest, tester *tester, t *testing.T){
  309. userModelBodyValidator,
  310. },
  311. },
  312. &userTest{
  313. initializers: []func(tester *tester){
  314. initUserDefault,
  315. },
  316. msg: "Read user unauthorized",
  317. method: "GET",
  318. endpoint: "/api/users/2",
  319. body: "",
  320. expStatus: http.StatusForbidden,
  321. expBody: http.StatusText(http.StatusForbidden) + "\n",
  322. validators: []func(c *userTest, tester *tester, t *testing.T){
  323. userBasicBodyValidator,
  324. },
  325. },
  326. }
  327. func TestHandleReadUser(t *testing.T) {
  328. testUserRequests(t, readUserTests, true)
  329. }
  330. var readUserContextsTests = []*userTest{
  331. &userTest{
  332. initializers: []func(tester *tester){
  333. initUserWithContexts,
  334. },
  335. msg: "Read user context selected successful",
  336. method: "GET",
  337. endpoint: "/api/users/1/contexts",
  338. body: "",
  339. expStatus: http.StatusOK,
  340. useCookie: true,
  341. expBody: `[{"name":"context-test","server":"https://localhost","cluster":"cluster-test","user":"test-admin","selected":true}]`,
  342. validators: []func(c *userTest, tester *tester, t *testing.T){
  343. userContextBodyValidator,
  344. },
  345. },
  346. &userTest{
  347. initializers: []func(tester *tester){
  348. func(tester *tester) {
  349. initUserDefault(tester)
  350. user, _ := tester.repo.User.ReadUserByEmail("belanger@getporter.dev")
  351. user.Contexts = ""
  352. user.RawKubeConfig = []byte("apiVersion: v1\nkind: Config\npreferences: {}\ncurrent-context: context-test\nclusters:\n- cluster:\n server: https://localhost\n name: cluster-test\ncontexts:\n- context:\n cluster: cluster-test\n user: test-admin\n name: context-test\nusers:\n- name: test-admin")
  353. tester.repo.User.UpdateUser(user)
  354. },
  355. },
  356. msg: "Read user context not selected successful",
  357. method: "GET",
  358. endpoint: "/api/users/1/contexts",
  359. body: "",
  360. expStatus: http.StatusOK,
  361. useCookie: true,
  362. expBody: `[{"name":"context-test","server":"https://localhost","cluster":"cluster-test","user":"test-admin","selected":false}]`,
  363. validators: []func(c *userTest, tester *tester, t *testing.T){
  364. userContextBodyValidator,
  365. },
  366. },
  367. }
  368. func TestHandleReadUserContexts(t *testing.T) {
  369. testUserRequests(t, readUserContextsTests, true)
  370. }
  371. var updateUserTests = []*userTest{
  372. &userTest{
  373. initializers: []func(tester *tester){
  374. initUserDefault,
  375. },
  376. msg: "Update user successful",
  377. method: "PUT",
  378. endpoint: "/api/users/1",
  379. body: `{"rawKubeConfig":"apiVersion: v1\nkind: Config\npreferences: {}\ncurrent-context: context-test\nclusters:\n- cluster:\n server: https://localhost\n name: cluster-test\ncontexts:\n- context:\n cluster: cluster-test\n user: test-admin\n name: context-test\nusers:\n- name: test-admin", "allowedContexts":[]}`,
  380. expStatus: http.StatusNoContent,
  381. expBody: "",
  382. useCookie: true,
  383. validators: []func(c *userTest, tester *tester, t *testing.T){
  384. func(c *userTest, tester *tester, t *testing.T) {
  385. req, err := http.NewRequest(
  386. "GET",
  387. "/api/users/1",
  388. strings.NewReader(""),
  389. )
  390. req.AddCookie(tester.cookie)
  391. if err != nil {
  392. t.Fatal(err)
  393. }
  394. rr2 := httptest.NewRecorder()
  395. tester.router.ServeHTTP(rr2, req)
  396. gotBody := &models.UserExternal{}
  397. expBody := &models.UserExternal{}
  398. json.Unmarshal(rr2.Body.Bytes(), gotBody)
  399. json.Unmarshal([]byte(`{"id":1,"email":"belanger@getporter.dev","contexts":[],"rawKubeConfig":"apiVersion: v1\nkind: Config\npreferences: {}\ncurrent-context: context-test\nclusters:\n- cluster:\n server: https://localhost\n name: cluster-test\ncontexts:\n- context:\n cluster: cluster-test\n user: test-admin\n name: context-test\nusers:\n- name: test-admin"}`), expBody)
  400. fmt.Println(rr2.Body.String())
  401. if !reflect.DeepEqual(gotBody, expBody) {
  402. t.Errorf("%s, handler returned wrong body: got %v want %v",
  403. "validator failed", gotBody, expBody)
  404. }
  405. },
  406. },
  407. },
  408. &userTest{
  409. initializers: []func(tester *tester){
  410. initUserDefault,
  411. },
  412. msg: "Update user successful without allowedContexts parameter",
  413. method: "PUT",
  414. endpoint: "/api/users/1",
  415. body: `{"rawKubeConfig":"apiVersion: v1\nkind: Config\npreferences: {}\ncurrent-context: context-test\nclusters:\n- cluster:\n server: https://localhost\n name: cluster-test\ncontexts:\n- context:\n cluster: cluster-test\n user: test-admin\n name: context-test\nusers:\n- name: test-admin"}`,
  416. expStatus: http.StatusNoContent,
  417. expBody: "",
  418. useCookie: true,
  419. validators: []func(c *userTest, tester *tester, t *testing.T){
  420. func(c *userTest, tester *tester, t *testing.T) {
  421. req, err := http.NewRequest(
  422. "GET",
  423. "/api/users/1",
  424. strings.NewReader(""),
  425. )
  426. req.AddCookie(tester.cookie)
  427. if err != nil {
  428. t.Fatal(err)
  429. }
  430. rr2 := httptest.NewRecorder()
  431. tester.router.ServeHTTP(rr2, req)
  432. gotBody := &models.UserExternal{}
  433. expBody := &models.UserExternal{}
  434. json.Unmarshal(rr2.Body.Bytes(), gotBody)
  435. json.Unmarshal([]byte(`{"id":1,"email":"belanger@getporter.dev","contexts":[],"rawKubeConfig":"apiVersion: v1\nkind: Config\npreferences: {}\ncurrent-context: context-test\nclusters:\n- cluster:\n server: https://localhost\n name: cluster-test\ncontexts:\n- context:\n cluster: cluster-test\n user: test-admin\n name: context-test\nusers:\n- name: test-admin"}`), expBody)
  436. if !reflect.DeepEqual(gotBody, expBody) {
  437. t.Errorf("%s, handler returned wrong body: got %v want %v",
  438. "validator failed", gotBody, expBody)
  439. }
  440. },
  441. },
  442. },
  443. &userTest{
  444. initializers: []func(tester *tester){
  445. initUserDefault,
  446. },
  447. msg: "Update user successful with allowedContexts",
  448. method: "PUT",
  449. endpoint: "/api/users/1",
  450. body: `{"rawKubeConfig":"apiVersion: v1\nkind: Config\npreferences: {}\ncurrent-context: context-test\nclusters:\n- cluster:\n server: https://localhost\n name: cluster-test\ncontexts:\n- context:\n cluster: cluster-test\n user: test-admin\n name: context-test\nusers:\n- name: test-admin", "allowedContexts":["context-test"]}`,
  451. expStatus: http.StatusNoContent,
  452. expBody: "",
  453. useCookie: true,
  454. validators: []func(c *userTest, tester *tester, t *testing.T){
  455. func(c *userTest, tester *tester, t *testing.T) {
  456. req, err := http.NewRequest(
  457. "GET",
  458. "/api/users/1",
  459. strings.NewReader(""),
  460. )
  461. req.AddCookie(tester.cookie)
  462. if err != nil {
  463. t.Fatal(err)
  464. }
  465. rr2 := httptest.NewRecorder()
  466. tester.router.ServeHTTP(rr2, req)
  467. gotBody := &models.UserExternal{}
  468. expBody := &models.UserExternal{}
  469. json.Unmarshal(rr2.Body.Bytes(), gotBody)
  470. json.Unmarshal([]byte(`{"id":1,"email":"belanger@getporter.dev","contexts":["context-test"],"rawKubeConfig":"apiVersion: v1\nkind: Config\npreferences: {}\ncurrent-context: context-test\nclusters:\n- cluster:\n server: https://localhost\n name: cluster-test\ncontexts:\n- context:\n cluster: cluster-test\n user: test-admin\n name: context-test\nusers:\n- name: test-admin"}`), expBody)
  471. if !reflect.DeepEqual(gotBody, expBody) {
  472. t.Errorf("%s, handler returned wrong body: got %v want %v",
  473. "validator failed", gotBody, expBody)
  474. }
  475. },
  476. },
  477. },
  478. &userTest{
  479. initializers: []func(tester *tester){
  480. initUserWithContexts,
  481. },
  482. msg: "Update user successful without rawKubeConfig",
  483. method: "PUT",
  484. endpoint: "/api/users/1",
  485. body: `{"allowedContexts":[]}`,
  486. expStatus: http.StatusNoContent,
  487. expBody: "",
  488. useCookie: true,
  489. validators: []func(c *userTest, tester *tester, t *testing.T){
  490. func(c *userTest, tester *tester, t *testing.T) {
  491. req, err := http.NewRequest(
  492. "GET",
  493. "/api/users/1",
  494. strings.NewReader(""),
  495. )
  496. req.AddCookie(tester.cookie)
  497. if err != nil {
  498. t.Fatal(err)
  499. }
  500. rr2 := httptest.NewRecorder()
  501. tester.router.ServeHTTP(rr2, req)
  502. gotBody := &models.UserExternal{}
  503. expBody := &models.UserExternal{}
  504. json.Unmarshal(rr2.Body.Bytes(), gotBody)
  505. json.Unmarshal([]byte(`{"id":1,"email":"belanger@getporter.dev","contexts":[],"rawKubeConfig":"apiVersion: v1\nkind: Config\npreferences: {}\ncurrent-context: context-test\nclusters:\n- cluster:\n server: https://localhost\n name: cluster-test\ncontexts:\n- context:\n cluster: cluster-test\n user: test-admin\n name: context-test\nusers:\n- name: test-admin"}`), expBody)
  506. if !reflect.DeepEqual(gotBody, expBody) {
  507. t.Errorf("%s, handler returned wrong body: got %v want %v",
  508. "validator failed", gotBody, expBody)
  509. }
  510. },
  511. },
  512. },
  513. &userTest{
  514. initializers: []func(tester *tester){
  515. initUserDefault,
  516. },
  517. msg: "Update user invalid id",
  518. method: "PUT",
  519. endpoint: "/api/users/alsdfjk",
  520. body: `{"rawKubeConfig":"apiVersion: v1\nkind: Config\npreferences: {}\ncurrent-context: context-test\nclusters:\n- cluster:\n server: https://localhost\n name: cluster-test\ncontexts:\n- context:\n cluster: cluster-test\n user: test-admin\n name: context-test\nusers:\n- name: test-admin", "allowedContexts":[]}`,
  521. expStatus: http.StatusForbidden,
  522. expBody: http.StatusText(http.StatusForbidden) + "\n",
  523. validators: []func(c *userTest, tester *tester, t *testing.T){
  524. userBasicBodyValidator,
  525. },
  526. },
  527. &userTest{
  528. initializers: []func(tester *tester){
  529. initUserDefault,
  530. },
  531. msg: "Update user bad kubeconfig",
  532. method: "PUT",
  533. endpoint: "/api/users/1",
  534. body: `{"rawKubeConfig":"notvalidyaml", "allowedContexts":[]}`,
  535. expStatus: http.StatusBadRequest,
  536. expBody: `{"code":600,"errors":["could not process request"]}`,
  537. useCookie: true,
  538. validators: []func(c *userTest, tester *tester, t *testing.T){
  539. userBasicBodyValidator,
  540. },
  541. },
  542. }
  543. func TestHandleUpdateUser(t *testing.T) {
  544. testUserRequests(t, updateUserTests, true)
  545. }
  546. var deleteUserTests = []*userTest{
  547. &userTest{
  548. initializers: []func(tester *tester){
  549. initUserDefault,
  550. },
  551. msg: "Delete user successful",
  552. method: "DELETE",
  553. endpoint: "/api/users/1",
  554. body: `{"password":"hello"}`,
  555. expStatus: http.StatusNoContent,
  556. expBody: "",
  557. useCookie: true,
  558. validators: []func(c *userTest, tester *tester, t *testing.T){
  559. func(c *userTest, tester *tester, t *testing.T) {
  560. req, err := http.NewRequest(
  561. "GET",
  562. "/api/users/1",
  563. strings.NewReader(""),
  564. )
  565. req.AddCookie(tester.cookie)
  566. if err != nil {
  567. t.Fatal(err)
  568. }
  569. rr2 := httptest.NewRecorder()
  570. tester.router.ServeHTTP(rr2, req)
  571. gotBody := &models.UserExternal{}
  572. expBody := &models.UserExternal{}
  573. if status := rr2.Code; status != 404 {
  574. t.Errorf("DELETE user validation, handler returned wrong status code: got %v want %v",
  575. status, 404)
  576. }
  577. json.Unmarshal(rr2.Body.Bytes(), gotBody)
  578. json.Unmarshal([]byte(`{"code":602,"errors":["could not find requested object"]}`), expBody)
  579. if !reflect.DeepEqual(gotBody, expBody) {
  580. t.Errorf("%s, handler returned wrong body: got %v want %v",
  581. "validator failed", gotBody, expBody)
  582. }
  583. },
  584. },
  585. },
  586. &userTest{
  587. initializers: []func(tester *tester){
  588. initUserDefault,
  589. },
  590. msg: "Delete user invalid id",
  591. method: "DELETE",
  592. endpoint: "/api/users/aldkjf",
  593. body: `{"password":"hello"}`,
  594. expStatus: http.StatusForbidden,
  595. expBody: http.StatusText(http.StatusForbidden) + "\n",
  596. validators: []func(c *userTest, tester *tester, t *testing.T){
  597. userBasicBodyValidator,
  598. },
  599. },
  600. &userTest{
  601. initializers: []func(tester *tester){
  602. initUserDefault,
  603. },
  604. msg: "Delete user missing password",
  605. method: "DELETE",
  606. endpoint: "/api/users/1",
  607. body: `{}`,
  608. expStatus: http.StatusUnprocessableEntity,
  609. expBody: `{"code":601,"errors":["required validation failed"]}`,
  610. useCookie: true,
  611. validators: []func(c *userTest, tester *tester, t *testing.T){
  612. userBasicBodyValidator,
  613. },
  614. },
  615. }
  616. func TestHandleDeleteUser(t *testing.T) {
  617. testUserRequests(t, deleteUserTests, true)
  618. }
  619. // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  620. func initUserDefault(tester *tester) {
  621. tester.createUserSession("belanger@getporter.dev", "hello")
  622. }
  623. func initUserWithContexts(tester *tester) {
  624. initUserDefault(tester)
  625. user, _ := tester.repo.User.ReadUserByEmail("belanger@getporter.dev")
  626. user.Contexts = "context-test"
  627. user.RawKubeConfig = []byte("apiVersion: v1\nkind: Config\npreferences: {}\ncurrent-context: context-test\nclusters:\n- cluster:\n server: https://localhost\n name: cluster-test\ncontexts:\n- context:\n cluster: cluster-test\n user: test-admin\n name: context-test\nusers:\n- name: test-admin")
  628. tester.repo.User.UpdateUser(user)
  629. }
  630. func userBasicBodyValidator(c *userTest, tester *tester, t *testing.T) {
  631. if body := tester.rr.Body.String(); strings.TrimSpace(body) != strings.TrimSpace(c.expBody) {
  632. t.Errorf("%s, handler returned wrong body: got %v want %v",
  633. c.msg, body, c.expBody)
  634. }
  635. }
  636. func userModelBodyValidator(c *userTest, tester *tester, t *testing.T) {
  637. gotBody := &models.UserExternal{}
  638. expBody := &models.UserExternal{}
  639. json.Unmarshal(tester.rr.Body.Bytes(), gotBody)
  640. json.Unmarshal([]byte(c.expBody), expBody)
  641. if !reflect.DeepEqual(gotBody, expBody) {
  642. t.Errorf("%s, handler returned wrong body: got %v want %v",
  643. c.msg, gotBody, expBody)
  644. }
  645. }
  646. func userContextBodyValidator(c *userTest, tester *tester, t *testing.T) {
  647. gotBody := &[]models.Context{}
  648. expBody := &[]models.Context{}
  649. json.Unmarshal(tester.rr.Body.Bytes(), gotBody)
  650. json.Unmarshal([]byte(c.expBody), expBody)
  651. if !reflect.DeepEqual(gotBody, expBody) {
  652. t.Errorf("%s, handler returned wrong body: got %v want %v",
  653. c.msg, gotBody, expBody)
  654. }
  655. }