user_handler_test.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534
  1. package api_test
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "net/http/httptest"
  6. "reflect"
  7. "strings"
  8. "testing"
  9. "github.com/go-test/deep"
  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":"belanger@getporter.dev"}`,
  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":"belanger@getporter.dev"}`,
  103. validators: []func(c *userTest, tester *tester, t *testing.T){
  104. userModelBodyValidator,
  105. },
  106. },
  107. &userTest{
  108. msg: "Create user invalid email",
  109. method: "POST",
  110. endpoint: "/api/users",
  111. body: `{
  112. "email": "notanemail",
  113. "password": "hello"
  114. }`,
  115. expStatus: http.StatusUnprocessableEntity,
  116. expBody: `{"code":601,"errors":["email validation failed"]}`,
  117. validators: []func(c *userTest, tester *tester, t *testing.T){
  118. userBasicBodyValidator,
  119. },
  120. },
  121. &userTest{
  122. msg: "Create user missing field",
  123. method: "POST",
  124. endpoint: "/api/users",
  125. body: `{
  126. "password": "hello"
  127. }`,
  128. expStatus: http.StatusUnprocessableEntity,
  129. expBody: `{"code":601,"errors":["required validation failed"]}`,
  130. validators: []func(c *userTest, tester *tester, t *testing.T){
  131. userBasicBodyValidator,
  132. },
  133. },
  134. &userTest{
  135. initializers: []func(tester *tester){
  136. initUserDefault,
  137. },
  138. msg: "Create user same email",
  139. method: "POST",
  140. endpoint: "/api/users",
  141. body: `{
  142. "email": "belanger@getporter.dev",
  143. "password": "hello"
  144. }`,
  145. expStatus: http.StatusUnprocessableEntity,
  146. expBody: `{"code":601,"errors":["email already taken"]}`,
  147. validators: []func(c *userTest, tester *tester, t *testing.T){
  148. userBasicBodyValidator,
  149. },
  150. },
  151. &userTest{
  152. msg: "Create user invalid field type",
  153. method: "POST",
  154. endpoint: "/api/users",
  155. body: `{
  156. "email": "belanger@getporter.dev",
  157. "password": 0
  158. }`,
  159. expStatus: http.StatusBadRequest,
  160. expBody: `{"code":600,"errors":["could not process request"]}`,
  161. validators: []func(c *userTest, tester *tester, t *testing.T){
  162. userBasicBodyValidator,
  163. },
  164. },
  165. }
  166. func TestHandleCreateUser(t *testing.T) {
  167. testUserRequests(t, createUserTests, true)
  168. }
  169. var createUserTestsWriteFail = []*userTest{
  170. &userTest{
  171. msg: "Create user db connection down",
  172. method: "POST",
  173. endpoint: "/api/users",
  174. body: `{
  175. "email": "belanger@getporter.dev",
  176. "password": "hello"
  177. }`,
  178. expStatus: http.StatusInternalServerError,
  179. expBody: `{"code":500,"errors":["could not read from database"]}`,
  180. validators: []func(c *userTest, tester *tester, t *testing.T){
  181. userBasicBodyValidator,
  182. },
  183. },
  184. }
  185. func TestHandleCreateUserWriteFail(t *testing.T) {
  186. testUserRequests(t, createUserTestsWriteFail, false)
  187. }
  188. var loginUserTests = []*userTest{
  189. &userTest{
  190. initializers: []func(tester *tester){
  191. initUserDefault,
  192. },
  193. msg: "Login user successful",
  194. method: "POST",
  195. endpoint: "/api/login",
  196. body: `{
  197. "email": "belanger@getporter.dev",
  198. "password": "hello"
  199. }`,
  200. expStatus: http.StatusOK,
  201. expBody: `{"id":1,"email":"belanger@getporter.dev"}`,
  202. validators: []func(c *userTest, tester *tester, t *testing.T){
  203. userBasicBodyValidator,
  204. },
  205. },
  206. &userTest{
  207. initializers: []func(tester *tester){
  208. initUserDefault,
  209. },
  210. msg: "Login user already logged in",
  211. method: "POST",
  212. endpoint: "/api/login",
  213. body: `{
  214. "email": "belanger@getporter.dev",
  215. "password": "hello"
  216. }`,
  217. expStatus: http.StatusOK,
  218. expBody: `{"id":1,"email":"belanger@getporter.dev"}`,
  219. useCookie: true,
  220. validators: []func(c *userTest, tester *tester, t *testing.T){
  221. userBasicBodyValidator,
  222. },
  223. },
  224. &userTest{
  225. msg: "Login user unregistered email",
  226. method: "POST",
  227. endpoint: "/api/login",
  228. body: `{
  229. "email": "belanger@getporter.dev",
  230. "password": "hello"
  231. }`,
  232. expStatus: http.StatusUnauthorized,
  233. expBody: `{"code":401,"errors":["email not registered"]}`,
  234. validators: []func(c *userTest, tester *tester, t *testing.T){
  235. userBasicBodyValidator,
  236. },
  237. },
  238. &userTest{
  239. initializers: []func(tester *tester){
  240. initUserDefault,
  241. },
  242. msg: "Login user incorrect password",
  243. method: "POST",
  244. endpoint: "/api/login",
  245. body: `{
  246. "email": "belanger@getporter.dev",
  247. "password": "notthepassword"
  248. }`,
  249. expStatus: http.StatusUnauthorized,
  250. expBody: `{"code":401,"errors":["incorrect password"]}`,
  251. useCookie: true,
  252. validators: []func(c *userTest, tester *tester, t *testing.T){
  253. userBasicBodyValidator,
  254. },
  255. },
  256. }
  257. func TestHandleLoginUser(t *testing.T) {
  258. testUserRequests(t, loginUserTests, true)
  259. }
  260. var logoutUserTests = []*userTest{
  261. &userTest{
  262. initializers: []func(tester *tester){
  263. initUserDefault,
  264. },
  265. msg: "Logout user successful",
  266. method: "POST",
  267. endpoint: "/api/logout",
  268. body: `{
  269. "email": "belanger@getporter.dev",
  270. "password": "hello"
  271. }`,
  272. expStatus: http.StatusOK,
  273. expBody: ``,
  274. useCookie: true,
  275. validators: []func(c *userTest, tester *tester, t *testing.T){
  276. func(c *userTest, tester *tester, t *testing.T) {
  277. req, err := http.NewRequest(
  278. "GET",
  279. "/api/users/1",
  280. strings.NewReader(""),
  281. )
  282. req.AddCookie(tester.cookie)
  283. if err != nil {
  284. t.Fatal(err)
  285. }
  286. rr2 := httptest.NewRecorder()
  287. tester.router.ServeHTTP(rr2, req)
  288. if status := rr2.Code; status != http.StatusForbidden {
  289. t.Errorf("%s, handler returned wrong status: got %v want %v",
  290. "validator failed", status, http.StatusForbidden)
  291. }
  292. },
  293. },
  294. },
  295. }
  296. func TestHandleLogoutUser(t *testing.T) {
  297. testUserRequests(t, logoutUserTests, true)
  298. }
  299. var readUserTests = []*userTest{
  300. &userTest{
  301. initializers: []func(tester *tester){
  302. initUserDefault,
  303. },
  304. msg: "Read user successful",
  305. method: "GET",
  306. endpoint: "/api/users/1",
  307. body: "",
  308. expStatus: http.StatusOK,
  309. expBody: `{"id":1,"email":"belanger@getporter.dev"}`,
  310. useCookie: true,
  311. validators: []func(c *userTest, tester *tester, t *testing.T){
  312. userModelBodyValidator,
  313. },
  314. },
  315. &userTest{
  316. initializers: []func(tester *tester){
  317. initUserDefault,
  318. },
  319. msg: "Read user unauthorized",
  320. method: "GET",
  321. endpoint: "/api/users/2",
  322. body: "",
  323. expStatus: http.StatusForbidden,
  324. expBody: http.StatusText(http.StatusForbidden) + "\n",
  325. validators: []func(c *userTest, tester *tester, t *testing.T){
  326. userBasicBodyValidator,
  327. },
  328. },
  329. }
  330. func TestHandleReadUser(t *testing.T) {
  331. testUserRequests(t, readUserTests, true)
  332. }
  333. var listUserProjectsTests = []*userTest{
  334. &userTest{
  335. initializers: []func(tester *tester){
  336. initUserDefault,
  337. initProject,
  338. },
  339. msg: "List user projects successful",
  340. method: "GET",
  341. endpoint: "/api/users/1/projects",
  342. body: "",
  343. expStatus: http.StatusOK,
  344. expBody: `[{"id":1,"name":"project-test","roles":[{"id":0,"kind":"admin","user_id":1,"project_id":1}]}]`,
  345. useCookie: true,
  346. validators: []func(c *userTest, tester *tester, t *testing.T){
  347. userProjectsListValidator,
  348. },
  349. },
  350. &userTest{
  351. initializers: []func(tester *tester){
  352. initUserDefault,
  353. },
  354. msg: "List user projects unauthorized",
  355. method: "GET",
  356. endpoint: "/api/users/2/projects",
  357. body: "",
  358. expStatus: http.StatusForbidden,
  359. expBody: http.StatusText(http.StatusForbidden) + "\n",
  360. validators: []func(c *userTest, tester *tester, t *testing.T){
  361. userBasicBodyValidator,
  362. },
  363. },
  364. }
  365. func TestHandleListUserProjects(t *testing.T) {
  366. testUserRequests(t, listUserProjectsTests, true)
  367. }
  368. var deleteUserTests = []*userTest{
  369. &userTest{
  370. initializers: []func(tester *tester){
  371. initUserDefault,
  372. },
  373. msg: "Delete user successful",
  374. method: "DELETE",
  375. endpoint: "/api/users/1",
  376. body: `{"password":"hello"}`,
  377. expStatus: http.StatusNoContent,
  378. expBody: "",
  379. useCookie: true,
  380. validators: []func(c *userTest, tester *tester, t *testing.T){
  381. func(c *userTest, tester *tester, t *testing.T) {
  382. req, err := http.NewRequest(
  383. "GET",
  384. "/api/users/1",
  385. strings.NewReader(""),
  386. )
  387. req.AddCookie(tester.cookie)
  388. if err != nil {
  389. t.Fatal(err)
  390. }
  391. rr2 := httptest.NewRecorder()
  392. tester.router.ServeHTTP(rr2, req)
  393. gotBody := &models.UserExternal{}
  394. expBody := &models.UserExternal{}
  395. if status := rr2.Code; status != 404 {
  396. t.Errorf("DELETE user validation, handler returned wrong status code: got %v want %v",
  397. status, 404)
  398. }
  399. json.Unmarshal(rr2.Body.Bytes(), gotBody)
  400. json.Unmarshal([]byte(`{"code":602,"errors":["could not find requested object"]}`), expBody)
  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: "Delete user invalid id",
  413. method: "DELETE",
  414. endpoint: "/api/users/aldkjf",
  415. body: `{"password":"hello"}`,
  416. expStatus: http.StatusForbidden,
  417. expBody: http.StatusText(http.StatusForbidden) + "\n",
  418. validators: []func(c *userTest, tester *tester, t *testing.T){
  419. userBasicBodyValidator,
  420. },
  421. },
  422. &userTest{
  423. initializers: []func(tester *tester){
  424. initUserDefault,
  425. },
  426. msg: "Delete user missing password",
  427. method: "DELETE",
  428. endpoint: "/api/users/1",
  429. body: `{}`,
  430. expStatus: http.StatusUnprocessableEntity,
  431. expBody: `{"code":601,"errors":["required validation failed"]}`,
  432. useCookie: true,
  433. validators: []func(c *userTest, tester *tester, t *testing.T){
  434. userBasicBodyValidator,
  435. },
  436. },
  437. }
  438. func TestHandleDeleteUser(t *testing.T) {
  439. testUserRequests(t, deleteUserTests, true)
  440. }
  441. // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  442. func initUserDefault(tester *tester) {
  443. tester.createUserSession("belanger@getporter.dev", "hello")
  444. }
  445. func userBasicBodyValidator(c *userTest, tester *tester, t *testing.T) {
  446. if body := tester.rr.Body.String(); strings.TrimSpace(body) != strings.TrimSpace(c.expBody) {
  447. t.Errorf("%s, handler returned wrong body: got %v want %v",
  448. c.msg, body, c.expBody)
  449. }
  450. }
  451. func userModelBodyValidator(c *userTest, tester *tester, t *testing.T) {
  452. gotBody := &models.UserExternal{}
  453. expBody := &models.UserExternal{}
  454. json.Unmarshal(tester.rr.Body.Bytes(), gotBody)
  455. json.Unmarshal([]byte(c.expBody), expBody)
  456. if !reflect.DeepEqual(gotBody, expBody) {
  457. t.Errorf("%s, handler returned wrong body: got %v want %v",
  458. c.msg, gotBody, expBody)
  459. }
  460. }
  461. func userContextBodyValidator(c *userTest, tester *tester, t *testing.T) {
  462. gotBody := &[]models.Context{}
  463. expBody := &[]models.Context{}
  464. json.Unmarshal(tester.rr.Body.Bytes(), gotBody)
  465. json.Unmarshal([]byte(c.expBody), expBody)
  466. if !reflect.DeepEqual(gotBody, expBody) {
  467. t.Errorf("%s, handler returned wrong body: got %v want %v",
  468. c.msg, gotBody, expBody)
  469. }
  470. }
  471. func userProjectsListValidator(c *userTest, tester *tester, t *testing.T) {
  472. gotBody := make([]*models.ProjectExternal, 0)
  473. expBody := make([]*models.ProjectExternal, 0)
  474. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  475. json.Unmarshal([]byte(c.expBody), &expBody)
  476. if diff := deep.Equal(gotBody, expBody); diff != nil {
  477. t.Errorf("handler returned wrong body:\n")
  478. t.Error(diff)
  479. }
  480. }