-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathservice_access_postgres_test.go
More file actions
69 lines (62 loc) · 2.29 KB
/
Copy pathservice_access_postgres_test.go
File metadata and controls
69 lines (62 loc) · 2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package kata_test
import (
"bytes"
"context"
"database/sql"
"net/http"
"net/http/httptest"
"strconv"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.kenn.io/kata"
"go.kenn.io/kata/internal/testenv"
)
func TestServicePostgresTransactionFenceRollsBackWithMutation(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
dsn, cleanup := testenv.NewPostgresContainer(t, ctx)
t.Cleanup(cleanup)
controller := &recordingAccessController{}
controller.transactionFence = func(ctx context.Context, tx kata.Transaction) error {
if _, err := tx.ExecContext(ctx,
`INSERT INTO kata.fence_markers(attempt) VALUES($1)`, 1); err != nil {
return err
}
return kata.ErrAccessDenied
}
service, err := kata.New(ctx, kata.Config{
DSN: dsn, Access: controller,
Postgres: kata.PostgresConfig{Schema: "kata", SchemaMode: kata.PostgresSchemaBootstrap},
})
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, service.Close()) })
project, err := service.EnsureProject(ctx, kata.ProjectSpec{
UID: "01HZNQ7VFPK1XGD8R5MABCD4EX", Name: "example-project",
})
require.NoError(t, err)
inspection, err := sql.Open("pgx", dsn)
require.NoError(t, err)
t.Cleanup(func() { require.NoError(t, inspection.Close()) })
_, err = inspection.ExecContext(ctx,
`CREATE TABLE kata.fence_markers (attempt BIGINT NOT NULL)`)
require.NoError(t, err)
request := httptest.NewRequest(http.MethodPost, "/api/v1/projects/"+
strconv.FormatInt(project.Project.ID, 10)+"/issues",
bytes.NewBufferString(`{"actor":"ignored","title":"must roll back"}`))
request.Header.Set("Content-Type", "application/json")
request = request.WithContext(kata.WithPrincipal(request.Context(), kata.Principal{
Subject: "user-123", Actor: "Example User",
}))
response := httptest.NewRecorder()
service.Handler().ServeHTTP(response, request)
assert.Equal(t, http.StatusNotFound, response.Code)
var markerCount, issueCount int
require.NoError(t, inspection.QueryRowContext(ctx,
`SELECT count(*) FROM kata.fence_markers`).Scan(&markerCount))
require.NoError(t, inspection.QueryRowContext(ctx,
`SELECT count(*) FROM kata.issues WHERE title = $1`, "must roll back").Scan(&issueCount))
assert.Zero(t, markerCount)
assert.Zero(t, issueCount)
}