project_handler_test.go 4.7 KB

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