Skip to content

Commit 3286539

Browse files
authored
feat: create default tenant (#15)
* feat: create default tenant * fix: set the default tenant ID
1 parent 3f459f8 commit 3286539

4 files changed

Lines changed: 30 additions & 72 deletions

File tree

cmd/bulwarkauth/config.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ func NewAppConfig() (*AppConfig, error) {
106106
config.RequestsPerSecond = getEnvAsInt("REQUESTS_PER_SECOND", 20)
107107
config.AuthenticationAttempts = getEnvAsInt("AUTHENTICATION_ATTEMPTS", 5)
108108
config.LockoutDurationInSecs = getEnvAsInt("LOCKOUT_DURATION_IN_SEC", 15*60)
109-
config.DefaultTenantID = getEnv("DEFAULT_TENANT_ID", "default")
110109

111110
return config, nil
112111
}

cmd/bulwarkauth/main.go

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ func main() {
9595
})
9696
wd, _ := os.Getwd()
9797
logger.Info("working directory: ", "dir", wd)
98-
defaultTenantID := config.DefaultTenantID
98+
defaultTenantID := "default"
9999
err = createDefaultTenantID(context.Background(), tenantService, defaultTenantID)
100100
if err != nil {
101101
panic(err)
@@ -155,15 +155,9 @@ func createDefaultTenantID(ctx context.Context, tenantService tenants.TenantServ
155155
}
156156

157157
if len(existingTenants) == 0 {
158-
return tenantService.CreateDefault(ctx, defaultTenantID)
158+
return tenantService.CreateDefault(ctx)
159159
}
160-
161-
for _, tenant := range existingTenants {
162-
if tenant.ID == defaultTenantID {
163-
return nil
164-
}
165-
}
166-
return tenantService.CreateDefault(ctx, defaultTenantID)
160+
return nil
167161
}
168162

169163
func corsSetting(service *echo.Echo, config *AppConfig, logger *slog.Logger) {

internal/tenants/tenants.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package tenants
22

33
import (
44
"context"
5+
"time"
56

67
"go.mongodb.org/mongo-driver/bson"
78
"go.mongodb.org/mongo-driver/mongo"
@@ -12,22 +13,24 @@ const (
1213
)
1314

1415
type Tenant struct {
15-
ID string `json:"id"`
16-
Name string `json:"name"`
17-
Description string `json:"description"`
18-
Domain string `json:"domain"`
16+
ID string `json:"id"`
17+
Name string `json:"name"`
18+
Description string `json:"description"`
19+
Domain string `json:"domain"`
20+
Created time.Time `json:"created"`
21+
Modified time.Time `json:"modified"`
1922
}
2023

2124
type TenantRepository interface {
2225
ReadAll(ctx context.Context) ([]Tenant, error)
2326
Read(ctx context.Context, tenantID string) (*Tenant, error)
24-
Create(ctx context.Context, tenantID string) error
27+
Create(ctx context.Context) error
2528
}
2629

2730
type TenantService interface {
2831
ListTenants(ctx context.Context) ([]Tenant, error)
2932
GetTenant(ctx context.Context, tenantID string) (*Tenant, error)
30-
CreateDefault(ctx context.Context, tenantID string) error
33+
CreateDefault(ctx context.Context) error
3134
}
3235

3336
type MongoDbTenantRepository struct {
@@ -74,12 +77,18 @@ func (t *MongoDbTenantRepository) Read(ctx context.Context, tenantID string) (*T
7477
return &tenant, nil
7578
}
7679

77-
func (t *MongoDbTenantRepository) Create(ctx context.Context, tenantID string) error {
80+
func (t *MongoDbTenantRepository) Create(ctx context.Context) error {
7881
collection := t.db.Collection(tenantCollection)
82+
7983
tenant := Tenant{
80-
ID: tenantID,
84+
ID: "default",
85+
Name: "default",
86+
Description: "default",
87+
Created: time.Now(),
88+
Modified: time.Now(),
8189
}
8290
_, err := collection.InsertOne(ctx, tenant)
91+
8392
return err
8493
}
8594

@@ -101,6 +110,6 @@ func (s *DefaultTenantService) GetTenant(ctx context.Context, tenantID string) (
101110
return s.repo.Read(ctx, tenantID)
102111
}
103112

104-
func (s *DefaultTenantService) CreateDefault(ctx context.Context, tenantID string) error {
105-
return s.repo.Create(ctx, tenantID)
113+
func (s *DefaultTenantService) CreateDefault(ctx context.Context) error {
114+
return s.repo.Create(ctx)
106115
}

internal/tenants/tenants_test.go

Lines changed: 8 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ func TestMongoDbTenantRepository_Create(t *testing.T) {
3333
db := client.Database("bulwark-test")
3434
tenantRepo := NewMongoDbTenantRepository(db)
3535

36-
tenantID := "tenant1"
37-
err = tenantRepo.Create(context.TODO(), tenantID)
36+
tenantID := "default"
37+
err = tenantRepo.Create(context.TODO())
3838
assert.NoError(t, err)
3939

4040
tenant, err := tenantRepo.Read(context.TODO(), tenantID)
@@ -66,8 +66,8 @@ func TestMongoDbTenantRepository_Read(t *testing.T) {
6666
db := client.Database("bulwark-test")
6767
tenantRepo := NewMongoDbTenantRepository(db)
6868

69-
tenantID := "tenant1"
70-
err = tenantRepo.Create(context.TODO(), tenantID)
69+
tenantID := "default"
70+
err = tenantRepo.Create(context.TODO())
7171
assert.NoError(t, err)
7272

7373
tenant, err := tenantRepo.Read(context.TODO(), tenantID)
@@ -76,43 +76,6 @@ func TestMongoDbTenantRepository_Read(t *testing.T) {
7676
assert.Equal(t, tenantID, tenant.ID)
7777
}
7878

79-
func TestMongoDbTenantRepository_ReadAll(t *testing.T) {
80-
mongodb := utils.NewMongoTestUtil()
81-
mongoServer, err := mongodb.CreateServer()
82-
if err != nil {
83-
t.Fatal(err)
84-
}
85-
defer mongoServer.Stop()
86-
87-
clientOptions := options.Client().ApplyURI(mongoServer.URI())
88-
client, err := mongo.Connect(context.TODO(), clientOptions)
89-
if err != nil {
90-
t.Fatal(err)
91-
}
92-
defer func() {
93-
err := client.Disconnect(context.TODO())
94-
if err != nil {
95-
t.Fatal(err)
96-
}
97-
}()
98-
99-
db := client.Database("bulwark-test")
100-
tenantRepo := NewMongoDbTenantRepository(db)
101-
102-
tenant1ID := "tenant1"
103-
tenant2ID := "tenant2"
104-
105-
err = tenantRepo.Create(context.TODO(), tenant1ID)
106-
assert.NoError(t, err)
107-
108-
err = tenantRepo.Create(context.TODO(), tenant2ID)
109-
assert.NoError(t, err)
110-
111-
tenants, err := tenantRepo.ReadAll(context.TODO())
112-
assert.NoError(t, err)
113-
assert.Len(t, tenants, 2)
114-
}
115-
11679
func TestDefaultTenantService_CreateDefault(t *testing.T) {
11780
mongodb := utils.NewMongoTestUtil()
11881
mongoServer, err := mongodb.CreateServer()
@@ -136,9 +99,8 @@ func TestDefaultTenantService_CreateDefault(t *testing.T) {
13699
db := client.Database("bulwark-test")
137100
tenantRepo := NewMongoDbTenantRepository(db)
138101
tenantService := NewDefaultTenantService(tenantRepo)
139-
140-
tenantID := "tenant1"
141-
err = tenantService.CreateDefault(context.TODO(), tenantID)
102+
tenantID := "default"
103+
err = tenantService.CreateDefault(context.TODO())
142104
assert.NoError(t, err)
143105

144106
tenant, err := tenantService.GetTenant(context.TODO(), tenantID)
@@ -171,16 +133,10 @@ func TestDefaultTenantService_ListTenants(t *testing.T) {
171133
tenantRepo := NewMongoDbTenantRepository(db)
172134
tenantService := NewDefaultTenantService(tenantRepo)
173135

174-
tenant1ID := "tenant1"
175-
tenant2ID := "tenant2"
176-
177-
err = tenantService.CreateDefault(context.TODO(), tenant1ID)
178-
assert.NoError(t, err)
179-
180-
err = tenantService.CreateDefault(context.TODO(), tenant2ID)
136+
err = tenantService.CreateDefault(context.TODO())
181137
assert.NoError(t, err)
182138

183139
tenants, err := tenantService.ListTenants(context.TODO())
184140
assert.NoError(t, err)
185-
assert.Len(t, tenants, 2)
141+
assert.Len(t, tenants, 1)
186142
}

0 commit comments

Comments
 (0)