project_handler_test.go 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. package api_test
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "reflect"
  6. "strings"
  7. "testing"
  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: ``,
  69. useCookie: true,
  70. validators: []func(c *projTest, tester *tester, t *testing.T){
  71. projectBasicBodyValidator,
  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":"Owner","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. // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  100. func initProject(tester *tester) {
  101. user, _ := tester.repo.User.ReadUserByEmail("belanger@getporter.dev")
  102. // handle write to the database
  103. projModel, _ := tester.repo.Project.CreateProject(&models.Project{
  104. Name: "project-test",
  105. })
  106. // create a new Role with the user as the owner
  107. tester.repo.Project.CreateProjectRole(projModel, &models.Role{
  108. UserID: user.ID,
  109. ProjectID: projModel.ID,
  110. Kind: "Owner",
  111. })
  112. }
  113. func projectBasicBodyValidator(c *projTest, tester *tester, t *testing.T) {
  114. if body := tester.rr.Body.String(); strings.TrimSpace(body) != strings.TrimSpace(c.expBody) {
  115. t.Errorf("%s, handler returned wrong body: got %v want %v",
  116. c.msg, body, c.expBody)
  117. }
  118. }
  119. func projectModelBodyValidator(c *projTest, tester *tester, t *testing.T) {
  120. gotBody := &models.ProjectExternal{}
  121. expBody := &models.ProjectExternal{}
  122. json.Unmarshal(tester.rr.Body.Bytes(), gotBody)
  123. json.Unmarshal([]byte(c.expBody), expBody)
  124. if !reflect.DeepEqual(gotBody, expBody) {
  125. t.Errorf("%s, handler returned wrong body: got %v want %v",
  126. c.msg, gotBody, expBody)
  127. }
  128. }