project_handler_test.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. package api_test
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strings"
  6. "testing"
  7. "github.com/go-test/deep"
  8. "github.com/porter-dev/porter/internal/models"
  9. )
  10. // ------------------------- TEST TYPES AND MAIN LOOP ------------------------- //
  11. type projTest struct {
  12. initializers []func(t *tester)
  13. msg string
  14. method string
  15. endpoint string
  16. body string
  17. expStatus int
  18. expBody string
  19. useCookie bool
  20. validators []func(c *projTest, tester *tester, t *testing.T)
  21. }
  22. func testProjRequests(t *testing.T, tests []*projTest, canQuery bool) {
  23. for _, c := range tests {
  24. // create a new tester
  25. tester := newTester(canQuery)
  26. // if there's an initializer, call it
  27. for _, init := range c.initializers {
  28. init(tester)
  29. }
  30. req, err := http.NewRequest(
  31. c.method,
  32. c.endpoint,
  33. strings.NewReader(c.body),
  34. )
  35. tester.req = req
  36. if c.useCookie {
  37. req.AddCookie(tester.cookie)
  38. }
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. tester.execute()
  43. rr := tester.rr
  44. // first, check that the status matches
  45. if status := rr.Code; status != c.expStatus {
  46. t.Errorf("%s, handler returned wrong status code: got %v want %v",
  47. c.msg, status, c.expStatus)
  48. }
  49. // if there's a validator, call it
  50. for _, validate := range c.validators {
  51. validate(c, tester, t)
  52. }
  53. }
  54. }
  55. // ------------------------- TEST FIXTURES AND FUNCTIONS ------------------------- //
  56. var createProjectTests = []*projTest{
  57. &projTest{
  58. initializers: []func(t *tester){
  59. initUserDefault,
  60. },
  61. msg: "Create project",
  62. method: "POST",
  63. endpoint: "/api/projects",
  64. body: `{
  65. "name": "project-test"
  66. }`,
  67. expStatus: http.StatusCreated,
  68. expBody: `{"id":1,"name":"project-test","roles":[{"id":0,"kind":"admin","user_id":1,"project_id":1}]}`,
  69. useCookie: true,
  70. validators: []func(c *projTest, tester *tester, t *testing.T){
  71. projectModelBodyValidator,
  72. },
  73. },
  74. }
  75. func TestHandleCreateProject(t *testing.T) {
  76. testProjRequests(t, createProjectTests, true)
  77. }
  78. var readProjectTests = []*projTest{
  79. &projTest{
  80. initializers: []func(t *tester){
  81. initUserDefault,
  82. initProject,
  83. },
  84. msg: "Read project",
  85. method: "GET",
  86. endpoint: "/api/projects/1",
  87. body: ``,
  88. expStatus: http.StatusOK,
  89. expBody: `{"id":1,"name":"project-test","roles":[{"id":0,"kind":"admin","user_id":1,"project_id":1}]}`,
  90. useCookie: true,
  91. validators: []func(c *projTest, tester *tester, t *testing.T){
  92. projectModelBodyValidator,
  93. },
  94. },
  95. }
  96. func TestHandleReadProject(t *testing.T) {
  97. testProjRequests(t, readProjectTests, true)
  98. }
  99. var deleteProjectTests = []*projTest{
  100. &projTest{
  101. initializers: []func(t *tester){
  102. initUserDefault,
  103. initProject,
  104. },
  105. msg: "Delete project",
  106. method: "DELETE",
  107. endpoint: "/api/projects/1",
  108. body: ``,
  109. expStatus: http.StatusOK,
  110. expBody: `{"id":1,"name":"project-test","roles":[{"id":0,"kind":"admin","user_id":1,"project_id":1}]}`,
  111. useCookie: true,
  112. validators: []func(c *projTest, tester *tester, t *testing.T){
  113. projectModelBodyValidator,
  114. },
  115. },
  116. }
  117. func TestHandleDeleteProject(t *testing.T) {
  118. testProjRequests(t, deleteProjectTests, true)
  119. }
  120. // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  121. func initProject(tester *tester) {
  122. user, _ := tester.repo.User.ReadUserByEmail("belanger@getporter.dev")
  123. // handle write to the database
  124. projModel, _ := tester.repo.Project.CreateProject(&models.Project{
  125. Name: "project-test",
  126. })
  127. // create a new Role with the user as the owner
  128. tester.repo.Project.CreateProjectRole(projModel, &models.Role{
  129. UserID: user.ID,
  130. ProjectID: projModel.ID,
  131. Kind: models.RoleAdmin,
  132. })
  133. }
  134. func projectBasicBodyValidator(c *projTest, tester *tester, t *testing.T) {
  135. if body := tester.rr.Body.String(); strings.TrimSpace(body) != strings.TrimSpace(c.expBody) {
  136. t.Errorf("%s, handler returned wrong body: got %v want %v",
  137. c.msg, body, c.expBody)
  138. }
  139. }
  140. func projectModelBodyValidator(c *projTest, tester *tester, t *testing.T) {
  141. gotBody := &models.ProjectExternal{}
  142. expBody := &models.ProjectExternal{}
  143. json.Unmarshal(tester.rr.Body.Bytes(), gotBody)
  144. json.Unmarshal([]byte(c.expBody), expBody)
  145. if diff := deep.Equal(gotBody, expBody); diff != nil {
  146. t.Errorf("handler returned wrong body:\n")
  147. t.Error(diff)
  148. }
  149. }