Răsfoiți Sursa

Added tests for allowlist

jnfrati 4 ani în urmă
părinte
comite
ef1e8324e9

+ 53 - 0
internal/repository/gorm/allowlist_test.go

@@ -0,0 +1,53 @@
+package gorm_test
+
+import (
+	"testing"
+)
+
+func TestUserEmailExistsOnAllowlist(t *testing.T) {
+	tester := &tester{
+		dbFileName: "./porter_create_gr.db",
+	}
+
+	setupTestEnv(tester, t)
+	initUser(tester, t)
+	initProject(tester, t)
+	initOAuthIntegration(tester, t)
+	defer cleanup(tester, t)
+
+	expected := true
+
+	found, err := tester.repo.Allowlist().UserEmailExists("some@email.com")
+
+	if err != nil {
+		t.Fatalf("%v\n", err)
+	}
+
+	if found != expected {
+		t.Errorf("expected found to be %t but got: %t", expected, found)
+	}
+}
+
+func TestUserDontExistsOnAllowList(t *testing.T) {
+	tester := &tester{
+		dbFileName: "./porter_create_gr.db",
+	}
+
+	setupTestEnv(tester, t)
+	initUser(tester, t)
+	initProject(tester, t)
+	initOAuthIntegration(tester, t)
+	defer cleanup(tester, t)
+
+	expected := false
+
+	found, err := tester.repo.Allowlist().UserEmailExists("nonexisting@email.com")
+
+	if err != nil {
+		t.Fatalf("%v\n", err)
+	}
+
+	if found != expected {
+		t.Errorf("expected found to be %t but got: %t", expected, found)
+	}
+}

+ 1 - 0
internal/repository/gorm/helpers_test.go

@@ -69,6 +69,7 @@ func setupTestEnv(tester *tester, t *testing.T) {
 		&models.KubeEvent{},
 		&models.KubeSubEvent{},
 		&models.Onboarding{},
+		&models.Allowlist{},
 		&ints.KubeIntegration{},
 		&ints.BasicIntegration{},
 		&ints.OIDCIntegration{},

+ 49 - 0
internal/repository/test/allowlist.go

@@ -0,0 +1,49 @@
+package test
+
+import (
+	"errors"
+	"fmt"
+
+	"github.com/porter-dev/porter/internal/models"
+	"github.com/porter-dev/porter/internal/repository"
+)
+
+// AllowlistRepository uses gorm.DB for querying the database
+type AllowlistRepository struct {
+	canQuery  bool
+	allowlist []*models.Allowlist
+}
+
+// NewAllowlistRepository returns a AllowListRepository which uses
+// gorm.DB for querying the database.
+func NewAllowlistRepository(canQuery bool) repository.AllowlistRepository {
+	return &AllowlistRepository{canQuery, []*models.Allowlist{
+		{
+			UserEmail: "some@email.com",
+		},
+	}}
+}
+
+func (repo *AllowlistRepository) UserEmailExists(email string) (bool, error) {
+	if !repo.canQuery {
+		return false, errors.New("cannot read database")
+	}
+
+	fmt.Println(len(repo.allowlist))
+
+	if len(repo.allowlist) == 0 {
+		return false, nil
+	}
+
+	founded := false
+
+	for _, allowed := range repo.allowlist {
+		fmt.Println(allowed.UserEmail)
+		if allowed.UserEmail == email {
+			founded = true
+			break
+		}
+	}
+
+	return founded, nil
+}

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

@@ -196,5 +196,6 @@ func NewRepository(canQuery bool, failingMethods ...string) repository.Repositor
 		onboarding:                NewProjectOnboardingRepository(canQuery),
 		ceToken:                   NewCredentialsExchangeTokenRepository(canQuery),
 		buildConfig:               NewBuildConfigRepository(canQuery),
+		allowlist:                 NewAllowlistRepository(canQuery),
 	}
 }