Skip to content

Commit 38189fa

Browse files
committed
Increase unit and integration test coverage for Consent Engine and PDP
1 parent 01adc10 commit 38189fa

25 files changed

Lines changed: 3021 additions & 144 deletions

File tree

exchange/consent-engine/go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/golang-jwt/jwt/v5 v5.3.0
77
github.com/google/uuid v1.6.0
88
github.com/gov-dx-sandbox/exchange/shared/config v0.0.0
9+
github.com/gov-dx-sandbox/exchange/shared/testutils v0.0.0
910
github.com/gov-dx-sandbox/exchange/shared/utils v0.0.0
1011
)
1112

@@ -65,4 +66,6 @@ replace github.com/gov-dx-sandbox/exchange/shared/constants => ./shared/constant
6566

6667
replace github.com/gov-dx-sandbox/exchange/shared/monitoring => ../shared/monitoring
6768

69+
replace github.com/gov-dx-sandbox/exchange/shared/testutils => ../shared/testutils
70+
6871
replace github.com/gov-dx-sandbox/exchange/shared/utils => ./shared/utils
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package database
2+
3+
import (
4+
"os"
5+
"testing"
6+
"time"
7+
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestNewDatabaseConfig(t *testing.T) {
12+
// Clear environment variables
13+
os.Unsetenv("CHOREO_OPENDIF_DATABASE_HOSTNAME")
14+
os.Unsetenv("CHOREO_OPENDIF_DATABASE_PORT")
15+
os.Unsetenv("CHOREO_OPENDIF_DATABASE_USERNAME")
16+
os.Unsetenv("CHOREO_OPENDIF_DATABASE_PASSWORD")
17+
os.Unsetenv("CHOREO_OPENDIF_DATABASE_DATABASENAME")
18+
os.Unsetenv("DB_SSLMODE")
19+
20+
config := NewDatabaseConfig()
21+
22+
assert.Equal(t, "localhost", config.Host)
23+
assert.Equal(t, "5432", config.Port)
24+
assert.Equal(t, "postgres", config.Username)
25+
assert.Equal(t, "password", config.Password)
26+
assert.Equal(t, "testdb", config.Database)
27+
assert.Equal(t, "prefer", config.SSLMode)
28+
assert.Equal(t, 25, config.MaxOpenConns)
29+
assert.Equal(t, 5, config.MaxIdleConns)
30+
assert.Equal(t, time.Hour, config.ConnMaxLifetime)
31+
assert.Equal(t, 30*time.Minute, config.ConnMaxIdleTime)
32+
assert.Equal(t, 5, config.MaxRetries)
33+
}
34+
35+
func TestNewDatabaseConfig_WithEnvVars(t *testing.T) {
36+
os.Setenv("CHOREO_OPENDIF_DATABASE_HOSTNAME", "test-host")
37+
os.Setenv("CHOREO_OPENDIF_DATABASE_PORT", "5433")
38+
os.Setenv("CHOREO_OPENDIF_DATABASE_USERNAME", "test-user")
39+
os.Setenv("CHOREO_OPENDIF_DATABASE_PASSWORD", "test-password")
40+
os.Setenv("CHOREO_OPENDIF_DATABASE_DATABASENAME", "test-db")
41+
os.Setenv("DB_SSLMODE", "require")
42+
defer func() {
43+
os.Unsetenv("CHOREO_OPENDIF_DATABASE_HOSTNAME")
44+
os.Unsetenv("CHOREO_OPENDIF_DATABASE_PORT")
45+
os.Unsetenv("CHOREO_OPENDIF_DATABASE_USERNAME")
46+
os.Unsetenv("CHOREO_OPENDIF_DATABASE_PASSWORD")
47+
os.Unsetenv("CHOREO_OPENDIF_DATABASE_DATABASENAME")
48+
os.Unsetenv("DB_SSLMODE")
49+
}()
50+
51+
config := NewDatabaseConfig()
52+
53+
assert.Equal(t, "test-host", config.Host)
54+
assert.Equal(t, "5433", config.Port)
55+
assert.Equal(t, "test-user", config.Username)
56+
assert.Equal(t, "test-password", config.Password)
57+
assert.Equal(t, "test-db", config.Database)
58+
assert.Equal(t, "require", config.SSLMode)
59+
}
60+
61+
// NOTE: Tests for ConnectGormDB with real database connections have been moved to
62+
// tests/integration/database/database_test.go as integration tests.
63+
// Unit tests should not use real database connections.
64+

exchange/consent-engine/v1/handlers/internal_handler_test.go

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -15,30 +15,19 @@ import (
1515
"github.com/google/uuid"
1616
"github.com/gov-dx-sandbox/exchange/consent-engine/v1/models"
1717
"github.com/gov-dx-sandbox/exchange/consent-engine/v1/services"
18+
"github.com/gov-dx-sandbox/exchange/shared/testutils"
1819
"github.com/stretchr/testify/assert"
1920
"github.com/stretchr/testify/require"
20-
"gorm.io/driver/postgres"
2121
"gorm.io/gorm"
2222
)
2323

24-
func setupTestService(t *testing.T) (*services.ConsentService, sqlmock.Sqlmock) {
25-
db, mock, err := sqlmock.New()
26-
require.NoError(t, err)
27-
28-
dialector := postgres.New(postgres.Config{
29-
Conn: db,
30-
DriverName: "postgres",
31-
})
32-
33-
gormDB, err := gorm.Open(dialector, &gorm.Config{
34-
SkipDefaultTransaction: true,
35-
})
36-
require.NoError(t, err)
24+
func setupTestService(t *testing.T) (*services.ConsentService, sqlmock.Sqlmock, func()) {
25+
db, mock, cleanup := testutils.SetupMockDB(t)
3726

38-
service, err := services.NewConsentService(gormDB, "http://localhost:5173")
27+
service, err := services.NewConsentService(db, "http://localhost:5173")
3928
require.NoError(t, err)
4029

41-
return service, mock
30+
return service, mock, cleanup
4231
}
4332

4433
func TestInternalHandler_HealthCheck(t *testing.T) {
@@ -113,14 +102,16 @@ func TestInternalHandler_CreateConsent_MethodNotAllowed(t *testing.T) {
113102
}
114103

115104
func TestInternalHandler_NewInternalHandler(t *testing.T) {
116-
service, _ := setupTestService(t)
105+
service, _, cleanup := setupTestService(t)
106+
defer cleanup()
117107
handler := NewInternalHandler(service)
118108
assert.NotNil(t, handler)
119109
assert.Equal(t, service, handler.consentService)
120110
}
121111

122112
func TestInternalHandler_GetConsent_Success_WithOwnerID(t *testing.T) {
123-
service, mock := setupTestService(t)
113+
service, mock, cleanup := setupTestService(t)
114+
defer cleanup()
124115
handler := NewInternalHandler(service)
125116

126117
id := uuid.New()
@@ -145,7 +136,8 @@ func TestInternalHandler_GetConsent_Success_WithOwnerID(t *testing.T) {
145136
}
146137

147138
func TestInternalHandler_GetConsent_Success_WithOwnerEmail(t *testing.T) {
148-
service, mock := setupTestService(t)
139+
service, mock, cleanup := setupTestService(t)
140+
defer cleanup()
149141
handler := NewInternalHandler(service)
150142

151143
id := uuid.New()
@@ -166,7 +158,8 @@ func TestInternalHandler_GetConsent_Success_WithOwnerEmail(t *testing.T) {
166158
}
167159

168160
func TestInternalHandler_GetConsent_NotFound(t *testing.T) {
169-
service, mock := setupTestService(t)
161+
service, mock, cleanup := setupTestService(t)
162+
defer cleanup()
170163
handler := NewInternalHandler(service)
171164

172165
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "consent_records"`)).
@@ -182,7 +175,8 @@ func TestInternalHandler_GetConsent_NotFound(t *testing.T) {
182175
}
183176

184177
func TestInternalHandler_GetConsent_ContextTimeout(t *testing.T) {
185-
service, mock := setupTestService(t)
178+
service, mock, cleanup := setupTestService(t)
179+
defer cleanup()
186180
handler := NewInternalHandler(service)
187181

188182
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Nanosecond)
@@ -202,7 +196,8 @@ func TestInternalHandler_GetConsent_ContextTimeout(t *testing.T) {
202196
}
203197

204198
func TestInternalHandler_CreateConsent_Success(t *testing.T) {
205-
service, mock := setupTestService(t)
199+
service, mock, cleanup := setupTestService(t)
200+
defer cleanup()
206201
handler := NewInternalHandler(service)
207202

208203
// Mock GetConsentInternalView returning not found - specific query for owner_id and app_id
@@ -235,7 +230,8 @@ func TestInternalHandler_CreateConsent_Success(t *testing.T) {
235230
}
236231

237232
func TestInternalHandler_CreateConsent_CreateFailed(t *testing.T) {
238-
service, mock := setupTestService(t)
233+
service, mock, cleanup := setupTestService(t)
234+
defer cleanup()
239235
handler := NewInternalHandler(service)
240236

241237
// Mock GetConsentInternalView returning not found - specific query
@@ -268,7 +264,8 @@ func TestInternalHandler_CreateConsent_CreateFailed(t *testing.T) {
268264
}
269265

270266
func TestInternalHandler_GetConsent_InternalError(t *testing.T) {
271-
service, mock := setupTestService(t)
267+
service, mock, cleanup := setupTestService(t)
268+
defer cleanup()
272269
handler := NewInternalHandler(service)
273270

274271
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "consent_records" WHERE owner_id = $1 AND app_id = $2 ORDER BY created_at DESC`)+".*"+regexp.QuoteMeta(` LIMIT $3`)).
@@ -285,7 +282,8 @@ func TestInternalHandler_GetConsent_InternalError(t *testing.T) {
285282
}
286283

287284
func TestInternalHandler_CreateConsent_InternalError(t *testing.T) {
288-
service, mock := setupTestService(t)
285+
service, mock, cleanup := setupTestService(t)
286+
defer cleanup()
289287
handler := NewInternalHandler(service)
290288

291289
mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "consent_records" WHERE owner_id = $1 AND app_id = $2 ORDER BY created_at DESC`)+".*"+regexp.QuoteMeta(` LIMIT $3`)).

exchange/consent-engine/v1/handlers/portal_handler_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ import (
1515
// setUserEmailInContext is a test helper to set user email in context
1616
// Uses the same context key as middleware.auth.go (userEmailKey = "userEmail")
1717
func setUserEmailInContext(ctx context.Context, email string) context.Context {
18-
return context.WithValue(ctx, contextKey("userEmail"), email)
18+
type contextKey string
19+
const userEmailKey contextKey = "userEmail"
20+
return context.WithValue(ctx, userEmailKey, email)
1921
}
2022

21-
type contextKey string
22-
2323
func TestPortalHandler_HealthCheck(t *testing.T) {
2424
handler := &PortalHandler{consentService: nil}
2525

0 commit comments

Comments
 (0)