2023-04-19 08:46:02 +00:00
|
|
|
package query
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"database/sql/driver"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2024-03-27 13:48:22 +00:00
|
|
|
db_mock "github.com/zitadel/zitadel/internal/database/mock"
|
2023-04-19 08:46:02 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
expectedDeviceAuthQueryC = `SELECT` +
|
2024-04-16 08:34:38 +00:00
|
|
|
` projections.device_auth_requests2.client_id,` +
|
|
|
|
` projections.device_auth_requests2.device_code,` +
|
|
|
|
` projections.device_auth_requests2.user_code,` +
|
|
|
|
` projections.device_auth_requests2.scopes,` +
|
|
|
|
` projections.device_auth_requests2.audience` +
|
|
|
|
` FROM projections.device_auth_requests2`
|
2023-04-19 08:46:02 +00:00
|
|
|
expectedDeviceAuthWhereUserCodeQueryC = expectedDeviceAuthQueryC +
|
2024-04-16 08:34:38 +00:00
|
|
|
` WHERE projections.device_auth_requests2.instance_id = $1` +
|
|
|
|
` AND projections.device_auth_requests2.user_code = $2`
|
2023-04-19 08:46:02 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-12-20 12:21:08 +00:00
|
|
|
expectedDeviceAuthQuery = regexp.QuoteMeta(expectedDeviceAuthQueryC)
|
|
|
|
expectedDeviceAuthWhereUserCodeQuery = regexp.QuoteMeta(expectedDeviceAuthWhereUserCodeQueryC)
|
|
|
|
expectedDeviceAuthValues = []driver.Value{
|
2023-04-19 08:46:02 +00:00
|
|
|
"client-id",
|
2023-12-20 12:21:08 +00:00
|
|
|
"device1",
|
|
|
|
"user-code",
|
2023-10-19 10:19:10 +00:00
|
|
|
database.TextArray[string]{"a", "b", "c"},
|
2024-04-03 06:06:21 +00:00
|
|
|
[]string{"projectID", "clientID"},
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
2023-12-20 12:21:08 +00:00
|
|
|
expectedDeviceAuth = &domain.AuthRequestDevice{
|
|
|
|
ClientID: "client-id",
|
|
|
|
DeviceCode: "device1",
|
|
|
|
UserCode: "user-code",
|
|
|
|
Scopes: []string{"a", "b", "c"},
|
2024-04-03 06:06:21 +00:00
|
|
|
Audience: []string{"projectID", "clientID"},
|
2023-04-19 08:46:02 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2023-12-20 12:21:08 +00:00
|
|
|
func TestQueries_DeviceAuthRequestByUserCode(t *testing.T) {
|
2024-03-27 13:48:22 +00:00
|
|
|
client, mock, err := sqlmock.New(sqlmock.ValueConverterOption(new(db_mock.TypeConverter)))
|
2023-04-19 08:46:02 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("failed to build mock client: %v", err)
|
|
|
|
}
|
|
|
|
defer client.Close()
|
|
|
|
|
2023-08-22 12:49:02 +00:00
|
|
|
mock.ExpectBegin()
|
2023-04-19 08:46:02 +00:00
|
|
|
mock.ExpectQuery(expectedDeviceAuthWhereUserCodeQuery).WillReturnRows(
|
2024-03-27 13:48:22 +00:00
|
|
|
mock.NewRows(deviceAuthSelectColumns).AddRow(expectedDeviceAuthValues...),
|
2023-04-19 08:46:02 +00:00
|
|
|
)
|
2023-08-22 12:49:02 +00:00
|
|
|
mock.ExpectCommit()
|
2023-04-19 08:46:02 +00:00
|
|
|
q := Queries{
|
|
|
|
client: &database.DB{DB: client},
|
|
|
|
}
|
2023-12-20 12:21:08 +00:00
|
|
|
got, err := q.DeviceAuthRequestByUserCode(context.TODO(), "789")
|
2023-04-19 08:46:02 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
assert.Equal(t, expectedDeviceAuth, got)
|
|
|
|
require.NoError(t, mock.ExpectationsWereMet())
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_prepareDeviceAuthQuery(t *testing.T) {
|
|
|
|
type want struct {
|
|
|
|
sqlExpectations sqlExpectation
|
|
|
|
err checkErr
|
|
|
|
}
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
want want
|
2023-12-20 12:21:08 +00:00
|
|
|
object *domain.AuthRequestDevice
|
2023-04-19 08:46:02 +00:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "success",
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueries(
|
|
|
|
expectedDeviceAuthQuery,
|
|
|
|
deviceAuthSelectColumns,
|
|
|
|
[][]driver.Value{expectedDeviceAuthValues},
|
|
|
|
),
|
|
|
|
},
|
|
|
|
object: expectedDeviceAuth,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "not found error",
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueryErr(
|
|
|
|
expectedDeviceAuthQuery,
|
|
|
|
sql.ErrNoRows,
|
|
|
|
),
|
|
|
|
err: func(err error) (error, bool) {
|
|
|
|
if !errors.Is(err, sql.ErrNoRows) {
|
|
|
|
return fmt.Errorf("err should be sql.ErrNoRows got: %w", err), false
|
|
|
|
}
|
|
|
|
return nil, true
|
|
|
|
},
|
|
|
|
},
|
2023-12-20 12:21:08 +00:00
|
|
|
object: nil,
|
2023-04-19 08:46:02 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "other error",
|
|
|
|
want: want{
|
|
|
|
sqlExpectations: mockQueryErr(
|
|
|
|
expectedDeviceAuthQuery,
|
|
|
|
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-12-20 12:21:08 +00:00
|
|
|
object: nil,
|
2023-04-19 08:46:02 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
assertPrepare(t, prepareDeviceAuthQuery, tt.object, tt.want.sqlExpectations, tt.want.err, defaultPrepareArgs...)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|