invite_handler_test.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. package api_test
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "strings"
  6. "testing"
  7. "time"
  8. "github.com/go-test/deep"
  9. "github.com/porter-dev/porter/internal/models"
  10. )
  11. // ------------------------- TEST TYPES AND MAIN LOOP ------------------------- //
  12. type inviteTest 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 *inviteTest, tester *tester, t *testing.T)
  22. }
  23. func testInviteRequests(t *testing.T, tests []*inviteTest, 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 createInviteTests = []*inviteTest{
  58. {
  59. initializers: []func(t *tester){
  60. initUserDefault,
  61. initProject,
  62. },
  63. msg: "Create invite",
  64. method: "POST",
  65. endpoint: "/api/projects/1/invites",
  66. body: `{"email":"test@test.it"}`,
  67. expStatus: http.StatusCreated,
  68. expBody: `{"id":1,"expired":false,"email":"test@test.it","accepted":false}`,
  69. useCookie: true,
  70. validators: []func(c *inviteTest, tester *tester, t *testing.T){
  71. func(c *inviteTest, tester *tester, t *testing.T) {
  72. // manually read the invite to get the expected token
  73. invite, _ := tester.repo.Invite().ReadInvite(1)
  74. gotBody := &models.InviteExternal{}
  75. expBody := &models.InviteExternal{
  76. Token: invite.Token,
  77. }
  78. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  79. json.Unmarshal([]byte(c.expBody), &expBody)
  80. if diff := deep.Equal(gotBody, expBody); diff != nil {
  81. t.Errorf("handler returned wrong body:\n")
  82. t.Error(diff)
  83. }
  84. },
  85. },
  86. },
  87. }
  88. func TestHandleCreateInvite(t *testing.T) {
  89. testInviteRequests(t, createInviteTests, true)
  90. }
  91. var listInvitesTest = []*inviteTest{
  92. {
  93. initializers: []func(t *tester){
  94. initUserDefault,
  95. initProject,
  96. initInvite,
  97. },
  98. msg: "List invites",
  99. method: "GET",
  100. endpoint: "/api/projects/1/invites",
  101. body: ``,
  102. expStatus: http.StatusOK,
  103. expBody: `[{"id":1,"expired":false,"email":"test@test.it","accepted":false}]`,
  104. useCookie: true,
  105. validators: []func(c *inviteTest, tester *tester, t *testing.T){
  106. func(c *inviteTest, tester *tester, t *testing.T) {
  107. // manually read the invite to get the expected token
  108. invite, _ := tester.repo.Invite().ReadInvite(1)
  109. gotBody := []*models.InviteExternal{}
  110. expBody := []*models.InviteExternal{}
  111. json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
  112. json.Unmarshal([]byte(c.expBody), &expBody)
  113. expBody[0].Token = invite.Token
  114. if diff := deep.Equal(gotBody, expBody); diff != nil {
  115. t.Errorf("handler returned wrong body:\n")
  116. t.Error(diff)
  117. }
  118. },
  119. },
  120. },
  121. }
  122. func TestHandleListInvites(t *testing.T) {
  123. testInviteRequests(t, listInvitesTest, true)
  124. }
  125. var acceptInviteTests = []*inviteTest{
  126. {
  127. initializers: []func(t *tester){
  128. initUserDefault,
  129. initUserAlt,
  130. initProject,
  131. initInvite,
  132. },
  133. msg: "Accept invite",
  134. method: "GET",
  135. endpoint: "/api/projects/1/invites/abcd",
  136. body: ``,
  137. expStatus: http.StatusFound,
  138. expBody: ``,
  139. useCookie: true,
  140. validators: []func(c *inviteTest, tester *tester, t *testing.T){
  141. func(c *inviteTest, tester *tester, t *testing.T) {
  142. user, err := tester.repo.User().ReadUserByEmail("test@test.it")
  143. if err != nil {
  144. t.Fatalf("%v\n", err)
  145. }
  146. projects, err := tester.repo.Project().ListProjectsByUserID(user.ID)
  147. if len(projects) != 1 {
  148. t.Fatalf("length of projects not 1\n")
  149. }
  150. if projects[0].ID != 1 {
  151. t.Fatalf("project id was not 1\n")
  152. }
  153. if projects[0].Name != "project-test" {
  154. t.Fatalf("project was not project-test\n")
  155. }
  156. },
  157. },
  158. },
  159. {
  160. initializers: []func(t *tester){
  161. initUserDefault,
  162. initUserAlt,
  163. initProject,
  164. initInvite,
  165. },
  166. msg: "Accept invite wrong token",
  167. method: "GET",
  168. endpoint: "/api/projects/1/invites/abcd1",
  169. body: ``,
  170. expStatus: http.StatusFound,
  171. expBody: ``,
  172. useCookie: true,
  173. validators: []func(c *inviteTest, tester *tester, t *testing.T){
  174. func(c *inviteTest, tester *tester, t *testing.T) {
  175. expRes := "/dashboard?error=Invalid+invite+token"
  176. if expRes != tester.rr.HeaderMap.Get("Location") {
  177. t.Fatalf("Redirect location not correct: expected %v, got %v\n", expRes, tester.rr.HeaderMap.Get("Location"))
  178. }
  179. },
  180. },
  181. },
  182. {
  183. initializers: []func(t *tester){
  184. initUserDefault,
  185. initProject,
  186. initInvite,
  187. },
  188. msg: "Accept invite wrong user",
  189. method: "GET",
  190. endpoint: "/api/projects/1/invites/abcd",
  191. body: ``,
  192. expStatus: http.StatusFound,
  193. expBody: ``,
  194. useCookie: true,
  195. validators: []func(c *inviteTest, tester *tester, t *testing.T){
  196. func(c *inviteTest, tester *tester, t *testing.T) {
  197. expRes := "/dashboard?error=Wrong+email+for+invite"
  198. if expRes != tester.rr.HeaderMap.Get("Location") {
  199. t.Fatalf("Redirect location not correct: expected %v, got %v\n", expRes, tester.rr.HeaderMap.Get("Location"))
  200. }
  201. },
  202. },
  203. },
  204. {
  205. initializers: []func(t *tester){
  206. initUserDefault,
  207. initUserAlt,
  208. initProject,
  209. initInviteExpiredToken,
  210. },
  211. msg: "Accept invite expired token",
  212. method: "GET",
  213. endpoint: "/api/projects/1/invites/abcd",
  214. body: ``,
  215. expStatus: http.StatusFound,
  216. expBody: ``,
  217. useCookie: true,
  218. validators: []func(c *inviteTest, tester *tester, t *testing.T){
  219. func(c *inviteTest, tester *tester, t *testing.T) {
  220. expRes := "/dashboard?error=Invite+has+expired"
  221. if expRes != tester.rr.HeaderMap.Get("Location") {
  222. t.Fatalf("Redirect location not correct: expected %v, got %v\n", expRes, tester.rr.HeaderMap.Get("Location"))
  223. }
  224. },
  225. },
  226. },
  227. }
  228. func TestHandleAcceptInvite(t *testing.T) {
  229. testInviteRequests(t, acceptInviteTests, true)
  230. }
  231. // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
  232. func initInvite(tester *tester) {
  233. proj, _ := tester.repo.Project().ReadProject(1)
  234. expiry := time.Now().Add(24 * time.Hour)
  235. invite := &models.Invite{
  236. Token: "abcd",
  237. Expiry: &expiry,
  238. Email: "test@test.it",
  239. ProjectID: proj.Model.ID,
  240. }
  241. tester.repo.Invite().CreateInvite(invite)
  242. }
  243. func initInviteExpiredToken(tester *tester) {
  244. proj, _ := tester.repo.Project().ReadProject(1)
  245. expiry := time.Now().Add(-1 * time.Hour)
  246. invite := &models.Invite{
  247. Token: "abcd",
  248. Expiry: &expiry,
  249. Email: "belanger@getporter.dev",
  250. ProjectID: proj.Model.ID,
  251. }
  252. tester.repo.Invite().CreateInvite(invite)
  253. }