Files

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

188 lines
4.4 KiB
Go
Raw Permalink Normal View History

// Copyright IBM Corp. 2020, 2026
2023-02-02 17:15:17 -08:00
// SPDX-License-Identifier: BUSL-1.1
package auth_test
2020-06-19 13:10:05 -04:00
import (
"context"
"database/sql"
2020-06-19 13:10:05 -04:00
"testing"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/db"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/db/timestamp"
"repo.anhonesthost.net/CyberCoveLLC/threshold/internal/iam"
2020-06-19 13:10:05 -04:00
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDB_AuthMethodIDTrigger(t *testing.T) {
const (
createTable = `
create table if not exists test_auth_method (
2020-06-22 13:54:55 -04:00
public_id wt_public_id primary key,
2021-04-08 20:54:32 -04:00
scope_id wt_public_id not null references iam_scope(public_id),
2023-12-21 14:58:21 -05:00
name wt_name,
create_time wt_timestamp,
update_time wt_timestamp,
state text
2020-06-19 13:10:05 -04:00
);
`
insert = `
insert into test_auth_method
2023-12-21 14:58:21 -05:00
(public_id, scope_id, create_time, update_time, state)
values
2023-12-21 14:58:21 -05:00
(@public_id, @scoped_id, @create_time, @update_time, @state);
2020-06-19 13:10:05 -04:00
`
addTriggers = `
create trigger
insert_auth_method_subtype
before
insert on test_auth_method
for each row execute procedure insert_auth_method_subtype();
`
baseTableQuery = `
select count(*) from auth_method where public_id = @public_id
2020-06-19 13:10:05 -04:00
`
testTableQuery = `
select count(*) from test_auth_method where public_id = @public_id
2020-06-19 13:10:05 -04:00
`
)
assert, require := assert.New(t), require.New(t)
ctx := context.Background()
conn, _ := db.TestSetup(t, "postgres")
2020-08-19 16:47:18 -04:00
wrapper := db.TestWrapper(t)
rw := db.New(conn)
_, err := rw.Exec(ctx, createTable, nil)
2020-06-19 13:10:05 -04:00
require.NoError(err)
_, err = rw.Exec(ctx, addTriggers, nil)
2020-06-19 13:10:05 -04:00
require.NoError(err)
2020-08-19 16:47:18 -04:00
org, _ := iam.TestScopes(t, iam.TestRepo(t, conn, wrapper))
2020-06-19 13:10:05 -04:00
id := "l1Ocw0TpHn800CekIxIXlmQqRDgFDfYl"
2023-12-21 14:58:21 -05:00
inserted, err := rw.Exec(
ctx,
insert,
[]any{
sql.Named("public_id", id),
sql.Named("scoped_id", org.GetPublicId()),
sql.Named("create_time", timestamp.Now()),
sql.Named("update_time", timestamp.Now()),
sql.Named("state", "active-public"),
},
)
2020-06-19 13:10:05 -04:00
require.NoError(err)
require.Equal(1, inserted)
2020-06-19 13:10:05 -04:00
type c struct {
Count int
}
var count c
2022-10-25 18:29:38 -05:00
rows, err := rw.Query(ctx, baseTableQuery, []any{sql.Named("public_id", id)})
2020-06-19 13:10:05 -04:00
require.NoError(err)
defer rows.Close()
for rows.Next() {
err := rw.ScanRows(ctx, rows, &count)
require.NoError(err)
}
2024-01-22 14:03:40 -05:00
assert.NoError(rows.Err())
assert.Equal(1, count.Count)
count.Count = 0
2022-10-25 18:29:38 -05:00
rows, err = rw.Query(ctx, testTableQuery, []any{sql.Named("public_id", id)})
2020-06-19 13:10:05 -04:00
require.NoError(err)
defer rows.Close()
for rows.Next() {
err := rw.ScanRows(ctx, rows, &count)
require.NoError(err)
}
2024-01-22 14:03:40 -05:00
assert.NoError(rows.Err())
assert.Equal(1, count.Count)
2020-06-19 13:10:05 -04:00
}
func TestDB_AuthAccountIDTrigger(t *testing.T) {
const (
createTable = `
create table if not exists test_auth_account (
2020-06-22 13:54:55 -04:00
public_id wt_public_id primary key,
auth_method_id wt_public_id not null,
2020-06-22 13:54:55 -04:00
scope_id wt_public_id not null
2020-06-19 13:10:05 -04:00
);
`
insert = `
insert into test_auth_account
(public_id, auth_method_id)
values
(?, ?);
2020-06-19 13:10:05 -04:00
`
addTriggers = `
create trigger
insert_auth_account_subtype
before
insert on test_auth_account
for each row execute procedure insert_auth_account_subtype();
`
baseTableQuery = `
select count(*) from auth_account where public_id = ?;
2020-06-19 13:10:05 -04:00
`
testTableQuery = `
select count(*) from test_auth_account where public_id = ?;
`
insertAuthMethod = `
insert into auth_method
2020-06-22 13:54:55 -04:00
(public_id, scope_id)
values
(?, ?);
2020-06-19 13:10:05 -04:00
`
)
assert, require := assert.New(t), require.New(t)
ctx := context.Background()
conn, _ := db.TestSetup(t, "postgres")
2020-08-19 16:47:18 -04:00
wrapper := db.TestWrapper(t)
repo := iam.TestRepo(t, conn, wrapper)
rw := db.New(conn)
_, err := rw.Exec(ctx, createTable, nil)
2020-06-19 13:10:05 -04:00
require.NoError(err)
_, err = rw.Exec(ctx, addTriggers, nil)
2020-06-19 13:10:05 -04:00
require.NoError(err)
2020-08-19 16:47:18 -04:00
org, _ := iam.TestScopes(t, repo)
meth_id := "31Ocw0TpHn800CekIxIXlmQqRDgFDfYl"
2022-10-25 18:29:38 -05:00
_, err = rw.Exec(ctx, insertAuthMethod, []any{meth_id, org.GetPublicId()})
require.NoError(err)
2020-06-19 13:10:05 -04:00
id := "l1Ocw0TpHn800CekIxIXlmQqRDgFDfYl"
2022-10-25 18:29:38 -05:00
_, err = rw.Exec(ctx, insert, []any{id, meth_id})
2020-06-19 13:10:05 -04:00
require.NoError(err)
type c struct {
Count int
}
var count c
2022-10-25 18:29:38 -05:00
rows, err := rw.Query(ctx, baseTableQuery, []any{id})
2020-06-19 13:10:05 -04:00
require.NoError(err)
defer rows.Close()
for rows.Next() {
err := rw.ScanRows(ctx, rows, &count)
require.NoError(err)
}
2024-01-22 14:03:40 -05:00
assert.NoError(rows.Err())
assert.Equal(1, count.Count)
count.Count = 0
2022-10-25 18:29:38 -05:00
rows, err = rw.Query(ctx, testTableQuery, []any{id})
2020-06-19 13:10:05 -04:00
require.NoError(err)
defer rows.Close()
for rows.Next() {
err := rw.ScanRows(ctx, rows, &count)
require.NoError(err)
}
2024-01-22 14:03:40 -05:00
assert.NoError(rows.Err())
assert.Equal(1, count.Count)
2020-06-19 13:10:05 -04:00
}