2026-04-06 15:48:37 +05:30
|
|
|
// Copyright IBM Corp. 2020, 2026
|
2023-02-02 17:15:17 -08:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
|
|
2025-06-20 15:31:57 -04:00
|
|
|
package auth_test
|
2020-06-19 13:10:05 -04:00
|
|
|
|
|
|
|
|
import (
|
2020-09-30 18:11:59 -04:00
|
|
|
"context"
|
2021-09-30 08:47:34 -04:00
|
|
|
"database/sql"
|
2020-06-19 13:10:05 -04:00
|
|
|
"testing"
|
|
|
|
|
|
2026-10-12 10:33:41 -07:00
|
|
|
"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 = `
|
2020-06-21 13:16:03 -04:00
|
|
|
insert into test_auth_method
|
2023-12-21 14:58:21 -05:00
|
|
|
(public_id, scope_id, create_time, update_time, state)
|
2020-06-21 13:16:03 -04:00
|
|
|
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 = `
|
2021-09-30 08:47:34 -04:00
|
|
|
select count(*) from auth_method where public_id = @public_id
|
2020-06-19 13:10:05 -04:00
|
|
|
`
|
|
|
|
|
testTableQuery = `
|
2021-09-30 08:47:34 -04:00
|
|
|
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)
|
|
|
|
|
|
2020-09-30 18:11:59 -04:00
|
|
|
ctx := context.Background()
|
2020-07-06 10:15:01 -07:00
|
|
|
conn, _ := db.TestSetup(t, "postgres")
|
2020-08-19 16:47:18 -04:00
|
|
|
wrapper := db.TestWrapper(t)
|
2020-09-30 18:11:59 -04:00
|
|
|
rw := db.New(conn)
|
|
|
|
|
_, err := rw.Exec(ctx, createTable, nil)
|
2020-06-19 13:10:05 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
2020-09-30 18:11:59 -04:00
|
|
|
_, 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-21 13:16:03 -04:00
|
|
|
|
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)
|
2020-09-30 18:11:59 -04:00
|
|
|
require.Equal(1, inserted)
|
2020-06-19 13:10:05 -04:00
|
|
|
|
2020-09-30 18:11:59 -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)
|
2020-09-30 18:11:59 -04:00
|
|
|
defer rows.Close()
|
|
|
|
|
for rows.Next() {
|
2022-01-05 14:38:38 -05:00
|
|
|
err := rw.ScanRows(ctx, rows, &count)
|
2020-09-30 18:11:59 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
}
|
2024-01-22 14:03:40 -05:00
|
|
|
assert.NoError(rows.Err())
|
2020-09-30 18:11:59 -04:00
|
|
|
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)
|
2020-09-30 18:11:59 -04:00
|
|
|
defer rows.Close()
|
|
|
|
|
for rows.Next() {
|
2022-01-05 14:38:38 -05:00
|
|
|
err := rw.ScanRows(ctx, rows, &count)
|
2020-09-30 18:11:59 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
}
|
2024-01-22 14:03:40 -05:00
|
|
|
assert.NoError(rows.Err())
|
2020-09-30 18:11:59 -04:00
|
|
|
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,
|
2020-06-21 13:16:03 -04:00
|
|
|
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 = `
|
2020-06-21 13:16:03 -04:00
|
|
|
insert into test_auth_account
|
2020-07-24 10:23:35 -04:00
|
|
|
(public_id, auth_method_id)
|
2020-06-21 13:16:03 -04:00
|
|
|
values
|
2021-09-30 08:47:34 -04:00
|
|
|
(?, ?);
|
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 = `
|
2021-09-30 08:47:34 -04:00
|
|
|
select count(*) from auth_account where public_id = ?;
|
2020-06-19 13:10:05 -04:00
|
|
|
`
|
|
|
|
|
testTableQuery = `
|
2021-09-30 08:47:34 -04:00
|
|
|
select count(*) from test_auth_account where public_id = ?;
|
2020-06-21 13:16:03 -04:00
|
|
|
`
|
|
|
|
|
insertAuthMethod = `
|
|
|
|
|
insert into auth_method
|
2020-06-22 13:54:55 -04:00
|
|
|
(public_id, scope_id)
|
2020-06-21 13:16:03 -04:00
|
|
|
values
|
2021-09-30 08:47:34 -04:00
|
|
|
(?, ?);
|
2020-06-19 13:10:05 -04:00
|
|
|
`
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert, require := assert.New(t), require.New(t)
|
|
|
|
|
|
2020-09-30 18:11:59 -04:00
|
|
|
ctx := context.Background()
|
2020-07-06 10:15:01 -07:00
|
|
|
conn, _ := db.TestSetup(t, "postgres")
|
2020-08-19 16:47:18 -04:00
|
|
|
wrapper := db.TestWrapper(t)
|
|
|
|
|
repo := iam.TestRepo(t, conn, wrapper)
|
2020-09-30 18:11:59 -04:00
|
|
|
rw := db.New(conn)
|
|
|
|
|
_, err := rw.Exec(ctx, createTable, nil)
|
2020-06-19 13:10:05 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
|
2020-09-30 18:11:59 -04:00
|
|
|
_, 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)
|
2020-06-21 13:16:03 -04:00
|
|
|
meth_id := "31Ocw0TpHn800CekIxIXlmQqRDgFDfYl"
|
2022-10-25 18:29:38 -05:00
|
|
|
_, err = rw.Exec(ctx, insertAuthMethod, []any{meth_id, org.GetPublicId()})
|
2020-06-21 13:16:03 -04:00
|
|
|
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)
|
|
|
|
|
|
2020-09-30 18:11:59 -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{id})
|
2020-06-19 13:10:05 -04:00
|
|
|
require.NoError(err)
|
2020-09-30 18:11:59 -04:00
|
|
|
defer rows.Close()
|
|
|
|
|
for rows.Next() {
|
2022-01-05 14:38:38 -05:00
|
|
|
err := rw.ScanRows(ctx, rows, &count)
|
2020-09-30 18:11:59 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
}
|
2024-01-22 14:03:40 -05:00
|
|
|
assert.NoError(rows.Err())
|
2020-09-30 18:11:59 -04:00
|
|
|
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)
|
2020-09-30 18:11:59 -04:00
|
|
|
defer rows.Close()
|
|
|
|
|
for rows.Next() {
|
2022-01-05 14:38:38 -05:00
|
|
|
err := rw.ScanRows(ctx, rows, &count)
|
2020-09-30 18:11:59 -04:00
|
|
|
require.NoError(err)
|
|
|
|
|
}
|
2024-01-22 14:03:40 -05:00
|
|
|
assert.NoError(rows.Err())
|
2020-09-30 18:11:59 -04:00
|
|
|
assert.Equal(1, count.Count)
|
2020-06-19 13:10:05 -04:00
|
|
|
}
|