Explorar el Código

Add and fix tests

Mauricio Araujo hace 2 años
padre
commit
89234a93c8

+ 59 - 2
api/server/handlers/user/create_test.go

@@ -1,6 +1,7 @@
 package user_test
 
 import (
+	"encoding/json"
 	"net/http"
 	"testing"
 
@@ -9,6 +10,7 @@ import (
 	"github.com/porter-dev/porter/api/server/shared/apitest"
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/internal/repository/test"
+	"github.com/stretchr/testify/assert"
 )
 
 func TestCreateUserSuccessful(t *testing.T) {
@@ -35,7 +37,17 @@ func TestCreateUserSuccessful(t *testing.T) {
 
 	handler.ServeHTTP(rr, req)
 
-	expUser := &types.CreateUserResponse{
+	// Use a struct that is the same as types.User but without the
+	// referral fields. This is because the referral code is randomly
+	// generated and is tested separately.
+	expUser := &struct {
+		ID            uint   `json:"id"`
+		Email         string `json:"email"`
+		EmailVerified bool   `json:"email_verified"`
+		FirstName     string `json:"first_name"`
+		LastName      string `json:"last_name"`
+		CompanyName   string `json:"company_name"`
+	}{
 		ID:            1,
 		FirstName:     "Mister",
 		LastName:      "Porter",
@@ -44,7 +56,14 @@ func TestCreateUserSuccessful(t *testing.T) {
 		EmailVerified: false,
 	}
 
-	gotUser := &types.CreateUserResponse{}
+	gotUser := &struct {
+		ID            uint   `json:"id"`
+		Email         string `json:"email"`
+		EmailVerified bool   `json:"email_verified"`
+		FirstName     string `json:"first_name"`
+		LastName      string `json:"last_name"`
+		CompanyName   string `json:"company_name"`
+	}{}
 
 	apitest.AssertResponseExpected(t, rr, expUser, gotUser)
 }
@@ -191,3 +210,41 @@ func TestFailingCreateSessionMethod(t *testing.T) {
 
 	apitest.AssertResponseInternalServerError(t, rr)
 }
+
+func TestCreateUserReferralCode(t *testing.T) {
+	req, rr := apitest.GetRequestAndRecorder(
+		t,
+		string(types.HTTPVerbPost),
+		"/api/users",
+		&types.CreateUserRequest{
+			FirstName:   "Mister",
+			LastName:    "Porter",
+			CompanyName: "Porter Technologies, Inc.",
+			Email:       "mrp@porter.run",
+			Password:    "somepassword",
+		},
+	)
+
+	config := apitest.LoadConfig(t)
+
+	handler := user.NewUserCreateHandler(
+		config,
+		shared.NewDefaultRequestDecoderValidator(config.Logger, config.Alerter),
+		shared.NewDefaultResultWriter(config.Logger, config.Alerter),
+	)
+
+	handler.ServeHTTP(rr, req)
+	gotUser := &types.CreateUserResponse{}
+
+	// apitest.AssertResponseExpected(t, rr, expUser, gotUser)
+	err := json.NewDecoder(rr.Body).Decode(gotUser)
+	if err != nil {
+		t.Fatal(err)
+	}
+
+	// This is the default lenth of a shortuuid
+	desiredLenth := 22
+	assert.NotEmpty(t, gotUser.ReferralCode, "referral code should not be empty")
+	assert.Len(t, gotUser.ReferralCode, desiredLenth, "referral code should be 20 characters long")
+	assert.Equal(t, gotUser.ReferralRewardClaimed, false, "referral reward claimed should be false for new user")
+}

+ 24 - 0
internal/repository/test/referrral.go

@@ -0,0 +1,24 @@
+package test
+
+import (
+	"errors"
+
+	"github.com/porter-dev/porter/internal/models"
+	"github.com/porter-dev/porter/internal/repository"
+)
+
+// ReferralRepository represents the set of queries on the Referral model
+type ReferralRepository struct{}
+
+// NewAppInstanceRepository returns the test AppInstanceRepository
+func NewReferralRepository() repository.ReferralRepository {
+	return &ReferralRepository{}
+}
+
+func (repo *ReferralRepository) CreateReferral(referral *models.Referral) (*models.Referral, error) {
+	return referral, errors.New("cannot read database")
+}
+
+func (repo *ReferralRepository) GetReferralCountByUserID(userID uint) (int, error) {
+	return 0, errors.New("cannot read database")
+}

+ 7 - 0
internal/repository/test/repository.go

@@ -59,6 +59,7 @@ type TestRepository struct {
 	githubWebhook             repository.GithubWebhookRepository
 	datastore                 repository.DatastoreRepository
 	appInstance               repository.AppInstanceRepository
+	referral                  repository.ReferralRepository
 }
 
 func (t *TestRepository) User() repository.UserRepository {
@@ -283,6 +284,11 @@ func (t *TestRepository) AppInstance() repository.AppInstanceRepository {
 	return t.appInstance
 }
 
+// Referral returns a test Referral
+func (t *TestRepository) Referral() repository.ReferralRepository {
+	return t.referral
+}
+
 // NewRepository returns a Repository which persists users in memory
 // and accepts a parameter that can trigger read/write errors
 func NewRepository(canQuery bool, failingMethods ...string) repository.Repository {
@@ -341,5 +347,6 @@ func NewRepository(canQuery bool, failingMethods ...string) repository.Repositor
 		githubWebhook:             NewGithubWebhookRepository(),
 		datastore:                 NewDatastoreRepository(),
 		appInstance:               NewAppInstanceRepository(),
+		referral:                  NewReferralRepository(),
 	}
 }