2020-06-19 13:10:05 -04:00
|
|
|
package auth
|
|
|
|
|
|
|
|
|
|
import (
|
2020-09-30 18:11:59 -04:00
|
|
|
"context"
|
2020-06-19 13:10:05 -04:00
|
|
|
"testing"
|
|
|
|
|
|
2020-08-10 17:15:02 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/db"
|
|
|
|
|
"github.com/hashicorp/boundary/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),
|
|
|
|
|
name wt_name
|
2020-06-19 13:10:05 -04:00
|
|
|
);
|
|
|
|
|
`
|
|
|
|
|
insert = `
|
2020-06-21 13:16:03 -04:00
|
|
|
insert into test_auth_method
|
2020-06-22 13:54:55 -04:00
|
|
|
(public_id, scope_id)
|
2020-06-21 13:16:03 -04:00
|
|
|
values
|
|
|
|
|
($1, $2);
|
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 = `
|
2020-06-22 13:54:55 -04:00
|
|
|
select count(*) from auth_method where public_id = $1;
|
2020-06-19 13:10:05 -04:00
|
|
|
`
|
|
|
|
|
testTableQuery = `
|
2020-06-22 13:54:55 -04:00
|
|
|
select count(*) from test_auth_method where public_id = $1;
|
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"
|
2020-09-30 18:11:59 -04:00
|
|
|
inserted, err := rw.Exec(ctx, insert, []interface{}{id, org.GetPublicId()})
|
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
|
|
|
|
|
rows, err := rw.Query(ctx, baseTableQuery, []interface{}{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() {
|
|
|
|
|
err := rw.ScanRows(rows, &count)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
}
|
|
|
|
|
assert.Equal(1, count.Count)
|
|
|
|
|
|
|
|
|
|
count.Count = 0
|
|
|
|
|
rows, err = rw.Query(ctx, testTableQuery, []interface{}{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() {
|
|
|
|
|
err := rw.ScanRows(rows, &count)
|
|
|
|
|
require.NoError(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,
|
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
|
2020-07-24 10:23:35 -04:00
|
|
|
($1, $2);
|
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 = `
|
2020-06-22 13:54:55 -04:00
|
|
|
select count(*) from auth_account where public_id = $1;
|
2020-06-19 13:10:05 -04:00
|
|
|
`
|
|
|
|
|
testTableQuery = `
|
2020-06-22 13:54:55 -04:00
|
|
|
select count(*) from test_auth_account where public_id = $1;
|
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
|
|
|
|
|
($1, $2);
|
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"
|
2020-09-30 18:11:59 -04:00
|
|
|
_, err = rw.Exec(ctx, insertAuthMethod, []interface{}{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"
|
2020-09-30 18:11:59 -04:00
|
|
|
_, err = rw.Exec(ctx, insert, []interface{}{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
|
|
|
|
|
rows, err := rw.Query(ctx, baseTableQuery, []interface{}{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() {
|
|
|
|
|
err := rw.ScanRows(rows, &count)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
}
|
|
|
|
|
assert.Equal(1, count.Count)
|
|
|
|
|
|
|
|
|
|
count.Count = 0
|
|
|
|
|
rows, err = rw.Query(ctx, testTableQuery, []interface{}{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() {
|
|
|
|
|
err := rw.ScanRows(rows, &count)
|
|
|
|
|
require.NoError(err)
|
|
|
|
|
}
|
|
|
|
|
assert.Equal(1, count.Count)
|
2020-06-19 13:10:05 -04:00
|
|
|
}
|