2023-01-25 08:49:41 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2023-12-08 14:30:55 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-01-25 08:49:41 +00:00
|
|
|
)
|
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
var (
|
|
|
|
notificationPolicyStmt = regexp.QuoteMeta(`SELECT projections.notification_policies.id,` +
|
|
|
|
` projections.notification_policies.sequence,` +
|
|
|
|
` projections.notification_policies.creation_date,` +
|
|
|
|
` projections.notification_policies.change_date,` +
|
|
|
|
` projections.notification_policies.resource_owner,` +
|
|
|
|
` projections.notification_policies.password_change,` +
|
|
|
|
` projections.notification_policies.is_default,` +
|
|
|
|
` projections.notification_policies.state` +
|
|
|
|
` FROM projections.notification_policies` +
|
|
|
|
` AS OF SYSTEM TIME '-1 ms'`)
|
|
|
|
notificationPolicyCols = []string{
|
|
|
|
"id",
|
|
|
|
"sequence",
|
|
|
|
"creation_date",
|
|
|
|
"change_date",
|
|
|
|
"resource_owner",
|
|
|
|
"password_change",
|
|
|
|
"is_default",
|
|
|
|
"state",
|
|
|
|
}
|
|
|
|
)
|
2023-01-25 08:49:41 +00:00
|
|
|
|
|
|
|
func Test_NotificationPolicyPrepares(t *testing.T) {
|
|
|
|
type want struct {
|
|
|
|
sqlExpectations sqlExpectation
|
|
|
|
err checkErr
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
prepare interface{}
|
|
|
|
want want
|
|
|
|
object interface{}
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "prepareNotificationPolicyQuery no result",
|
|
|
|
prepare: prepareNotificationPolicyQuery,
|
|
|
|
want: want{
|
2023-08-22 10:49:22 +00:00
|
|
|
sqlExpectations: mockQueriesScanErr(
|
2023-01-25 08:49:41 +00:00
|
|
|
notificationPolicyStmt,
|
|
|
|
nil,
|
|
|
|
nil,
|
|
|
|
),
|
|
|
|
err: func(err error) (error, bool) {
|
2023-12-08 14:30:55 +00:00
|
|
|
if !zerrors.IsNotFound(err) {
|
2023-01-25 08:49:41 +00:00
|
|
|
return fmt.Errorf("err should be NotFoundError got: %w", err), false
|
|
|
|
}
|
|
|
|
return nil, true
|
|
|
|
},
|
|
|
|
},
|
|
|
|
object: (*NotificationPolicy)(nil),
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareNotificationPolicyQuery found",
|
|
|
|
prepare: prepareNotificationPolicyQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQuery(
|
|
|
|
notificationPolicyStmt,
|
2023-02-27 21:36:43 +00:00
|
|
|
notificationPolicyCols,
|
2023-01-25 08:49:41 +00:00
|
|
|
[]driver.Value{
|
|
|
|
"pol-id",
|
|
|
|
uint64(20211109),
|
|
|
|
testNow,
|
|
|
|
testNow,
|
|
|
|
"ro",
|
|
|
|
true,
|
|
|
|
true,
|
|
|
|
domain.PolicyStateActive,
|
|
|
|
},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: &NotificationPolicy{
|
|
|
|
ID: "pol-id",
|
|
|
|
CreationDate: testNow,
|
|
|
|
ChangeDate: testNow,
|
|
|
|
Sequence: 20211109,
|
|
|
|
ResourceOwner: "ro",
|
|
|
|
State: domain.PolicyStateActive,
|
|
|
|
PasswordChange: true,
|
|
|
|
IsDefault: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "prepareNotificationPolicyQuery sql err",
|
|
|
|
prepare: prepareNotificationPolicyQuery,
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueryErr(
|
|
|
|
notificationPolicyStmt,
|
|
|
|
sql.ErrConnDone,
|
|
|
|
),
|
|
|
|
err: func(err error) (error, bool) {
|
|
|
|
if !errors.Is(err, sql.ErrConnDone) {
|
|
|
|
return fmt.Errorf("err should be sql.ErrConnDone got: %w", err), false
|
|
|
|
}
|
|
|
|
return nil, true
|
|
|
|
},
|
|
|
|
},
|
2023-08-22 10:49:22 +00:00
|
|
|
object: (*NotificationPolicy)(nil),
|
2023-01-25 08:49:41 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
2023-02-27 21:36:43 +00:00
|
|
|
assertPrepare(t, tt.prepare, tt.object, tt.want.sqlExpectations, tt.want.err, defaultPrepareArgs...)
|
2023-01-25 08:49:41 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|