| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297 |
- package api_test
- import (
- "encoding/json"
- "net/http"
- "strings"
- "testing"
- "time"
- "github.com/go-test/deep"
- "github.com/porter-dev/porter/internal/models"
- )
- // ------------------------- TEST TYPES AND MAIN LOOP ------------------------- //
- type inviteTest struct {
- initializers []func(t *tester)
- msg string
- method string
- endpoint string
- body string
- expStatus int
- expBody string
- useCookie bool
- validators []func(c *inviteTest, tester *tester, t *testing.T)
- }
- func testInviteRequests(t *testing.T, tests []*inviteTest, canQuery bool) {
- for _, c := range tests {
- // create a new tester
- tester := newTester(canQuery)
- // if there's an initializer, call it
- for _, init := range c.initializers {
- init(tester)
- }
- req, err := http.NewRequest(
- c.method,
- c.endpoint,
- strings.NewReader(c.body),
- )
- tester.req = req
- if c.useCookie {
- req.AddCookie(tester.cookie)
- }
- if err != nil {
- t.Fatal(err)
- }
- tester.execute()
- rr := tester.rr
- // first, check that the status matches
- if status := rr.Code; status != c.expStatus {
- t.Errorf("%s, handler returned wrong status code: got %v want %v",
- c.msg, status, c.expStatus)
- }
- // if there's a validator, call it
- for _, validate := range c.validators {
- validate(c, tester, t)
- }
- }
- }
- // ------------------------- TEST FIXTURES AND FUNCTIONS ------------------------- //
- var createInviteTests = []*inviteTest{
- {
- initializers: []func(t *tester){
- initUserDefault,
- initProject,
- },
- msg: "Create invite",
- method: "POST",
- endpoint: "/api/projects/1/invites",
- body: `{"email":"test@test.it"}`,
- expStatus: http.StatusCreated,
- expBody: `{"id":1,"expired":false,"email":"test@test.it","accepted":false}`,
- useCookie: true,
- validators: []func(c *inviteTest, tester *tester, t *testing.T){
- func(c *inviteTest, tester *tester, t *testing.T) {
- // manually read the invite to get the expected token
- invite, _ := tester.repo.Invite().ReadInvite(1)
- gotBody := &models.InviteExternal{}
- expBody := &models.InviteExternal{
- Token: invite.Token,
- }
- json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
- json.Unmarshal([]byte(c.expBody), &expBody)
- if diff := deep.Equal(gotBody, expBody); diff != nil {
- t.Errorf("handler returned wrong body:\n")
- t.Error(diff)
- }
- },
- },
- },
- }
- func TestHandleCreateInvite(t *testing.T) {
- testInviteRequests(t, createInviteTests, true)
- }
- var listInvitesTest = []*inviteTest{
- {
- initializers: []func(t *tester){
- initUserDefault,
- initProject,
- initInvite,
- },
- msg: "List invites",
- method: "GET",
- endpoint: "/api/projects/1/invites",
- body: ``,
- expStatus: http.StatusOK,
- expBody: `[{"id":1,"expired":false,"email":"test@test.it","accepted":false}]`,
- useCookie: true,
- validators: []func(c *inviteTest, tester *tester, t *testing.T){
- func(c *inviteTest, tester *tester, t *testing.T) {
- // manually read the invite to get the expected token
- invite, _ := tester.repo.Invite().ReadInvite(1)
- gotBody := []*models.InviteExternal{}
- expBody := []*models.InviteExternal{}
- json.Unmarshal(tester.rr.Body.Bytes(), &gotBody)
- json.Unmarshal([]byte(c.expBody), &expBody)
- expBody[0].Token = invite.Token
- if diff := deep.Equal(gotBody, expBody); diff != nil {
- t.Errorf("handler returned wrong body:\n")
- t.Error(diff)
- }
- },
- },
- },
- }
- func TestHandleListInvites(t *testing.T) {
- testInviteRequests(t, listInvitesTest, true)
- }
- var acceptInviteTests = []*inviteTest{
- {
- initializers: []func(t *tester){
- initUserDefault,
- initUserAlt,
- initProject,
- initInvite,
- },
- msg: "Accept invite",
- method: "GET",
- endpoint: "/api/projects/1/invites/abcd",
- body: ``,
- expStatus: http.StatusFound,
- expBody: ``,
- useCookie: true,
- validators: []func(c *inviteTest, tester *tester, t *testing.T){
- func(c *inviteTest, tester *tester, t *testing.T) {
- user, err := tester.repo.User().ReadUserByEmail("test@test.it")
- if err != nil {
- t.Fatalf("%v\n", err)
- }
- projects, err := tester.repo.Project().ListProjectsByUserID(user.ID)
- if len(projects) != 1 {
- t.Fatalf("length of projects not 1\n")
- }
- if projects[0].ID != 1 {
- t.Fatalf("project id was not 1\n")
- }
- if projects[0].Name != "project-test" {
- t.Fatalf("project was not project-test\n")
- }
- },
- },
- },
- {
- initializers: []func(t *tester){
- initUserDefault,
- initUserAlt,
- initProject,
- initInvite,
- },
- msg: "Accept invite wrong token",
- method: "GET",
- endpoint: "/api/projects/1/invites/abcd1",
- body: ``,
- expStatus: http.StatusFound,
- expBody: ``,
- useCookie: true,
- validators: []func(c *inviteTest, tester *tester, t *testing.T){
- func(c *inviteTest, tester *tester, t *testing.T) {
- expRes := "/dashboard?error=Invalid+invite+token"
- if expRes != tester.rr.HeaderMap.Get("Location") {
- t.Fatalf("Redirect location not correct: expected %v, got %v\n", expRes, tester.rr.HeaderMap.Get("Location"))
- }
- },
- },
- },
- {
- initializers: []func(t *tester){
- initUserDefault,
- initProject,
- initInvite,
- },
- msg: "Accept invite wrong user",
- method: "GET",
- endpoint: "/api/projects/1/invites/abcd",
- body: ``,
- expStatus: http.StatusFound,
- expBody: ``,
- useCookie: true,
- validators: []func(c *inviteTest, tester *tester, t *testing.T){
- func(c *inviteTest, tester *tester, t *testing.T) {
- expRes := "/dashboard?error=Wrong+email+for+invite"
- if expRes != tester.rr.HeaderMap.Get("Location") {
- t.Fatalf("Redirect location not correct: expected %v, got %v\n", expRes, tester.rr.HeaderMap.Get("Location"))
- }
- },
- },
- },
- {
- initializers: []func(t *tester){
- initUserDefault,
- initUserAlt,
- initProject,
- initInviteExpiredToken,
- },
- msg: "Accept invite expired token",
- method: "GET",
- endpoint: "/api/projects/1/invites/abcd",
- body: ``,
- expStatus: http.StatusFound,
- expBody: ``,
- useCookie: true,
- validators: []func(c *inviteTest, tester *tester, t *testing.T){
- func(c *inviteTest, tester *tester, t *testing.T) {
- expRes := "/dashboard?error=Invite+has+expired"
- if expRes != tester.rr.HeaderMap.Get("Location") {
- t.Fatalf("Redirect location not correct: expected %v, got %v\n", expRes, tester.rr.HeaderMap.Get("Location"))
- }
- },
- },
- },
- }
- func TestHandleAcceptInvite(t *testing.T) {
- testInviteRequests(t, acceptInviteTests, true)
- }
- // ------------------------- INITIALIZERS AND VALIDATORS ------------------------- //
- func initInvite(tester *tester) {
- proj, _ := tester.repo.Project().ReadProject(1)
- expiry := time.Now().Add(24 * time.Hour)
- invite := &models.Invite{
- Token: "abcd",
- Expiry: &expiry,
- Email: "test@test.it",
- ProjectID: proj.Model.ID,
- }
- tester.repo.Invite().CreateInvite(invite)
- }
- func initInviteExpiredToken(tester *tester) {
- proj, _ := tester.repo.Project().ReadProject(1)
- expiry := time.Now().Add(-1 * time.Hour)
- invite := &models.Invite{
- Token: "abcd",
- Expiry: &expiry,
- Email: "belanger@getporter.dev",
- ProjectID: proj.Model.ID,
- }
- tester.repo.Invite().CreateInvite(invite)
- }
|