Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions pkg/cli/account_api_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestAPIKeyService_ListAPIKeys(t *testing.T) {
name: "list all keys",
search: "",
setupAcc: func(acc *portalsdkmocks.MockAccountAPI) {
acc.EXPECT().ListAPIKeys(context.Background(), mock.Anything).
acc.EXPECT().ListAPIKeys(mock.Anything, mock.Anything).
Return([]*portalsdk.APIKey{
newTestAPIKey("key1", "00000000-0000-0000-0000-000000000001"),
newTestAPIKey("key2", "00000000-0000-0000-0000-000000000002"),
Expand All @@ -57,7 +57,7 @@ func TestAPIKeyService_ListAPIKeys(t *testing.T) {
name: "list with search",
search: "my-key",
setupAcc: func(acc *portalsdkmocks.MockAccountAPI) {
acc.EXPECT().ListAPIKeys(context.Background(), mock.Anything).
acc.EXPECT().ListAPIKeys(mock.Anything, mock.Anything).
Return([]*portalsdk.APIKey{
newTestAPIKey("my-key", "00000000-0000-0000-0000-000000000003"),
}, 1, nil)
Expand All @@ -68,7 +68,7 @@ func TestAPIKeyService_ListAPIKeys(t *testing.T) {
name: "empty list",
search: "",
setupAcc: func(acc *portalsdkmocks.MockAccountAPI) {
acc.EXPECT().ListAPIKeys(context.Background(), mock.Anything).
acc.EXPECT().ListAPIKeys(mock.Anything, mock.Anything).
Return([]*portalsdk.APIKey{}, 0, nil)
},
wantCount: 0,
Expand All @@ -77,7 +77,7 @@ func TestAPIKeyService_ListAPIKeys(t *testing.T) {
name: "api error",
search: "",
setupAcc: func(acc *portalsdkmocks.MockAccountAPI) {
acc.EXPECT().ListAPIKeys(context.Background(), mock.Anything).
acc.EXPECT().ListAPIKeys(mock.Anything, mock.Anything).
Return(nil, 0, fmt.Errorf("server error"))
},
wantErr: true,
Expand Down Expand Up @@ -115,15 +115,15 @@ func TestAPIKeyService_CreateAPIKey(t *testing.T) {
name: "successful create",
keyName: "new-key",
setupAcc: func(acc *portalsdkmocks.MockAccountAPI) {
acc.EXPECT().CreateAPIKey(context.Background(), "new-key").
acc.EXPECT().CreateAPIKey(mock.Anything, "new-key").
Return(portalsdk.NewAPIKey("new-key", "new-key-token"), nil)
},
},
{
name: "create fails",
keyName: "bad-key",
setupAcc: func(acc *portalsdkmocks.MockAccountAPI) {
acc.EXPECT().CreateAPIKey(context.Background(), "bad-key").
acc.EXPECT().CreateAPIKey(mock.Anything, "bad-key").
Return(nil, fmt.Errorf("duplicate key name"))
},
wantErr: true,
Expand Down Expand Up @@ -166,7 +166,7 @@ func TestAPIKeyService_DeleteAPIKey(t *testing.T) {
force: false,
authToken: makeAPIKeyJWT("other-uuid", "api"),
setupAcc: func(acc *portalsdkmocks.MockAccountAPI) {
acc.EXPECT().DeleteAPIKey(context.Background(), uuidStr).Return(nil)
acc.EXPECT().DeleteAPIKey(mock.Anything, uuidStr).Return(nil)
},
},
{
Expand All @@ -175,9 +175,9 @@ func TestAPIKeyService_DeleteAPIKey(t *testing.T) {
force: false,
authToken: makeAPIKeyJWT("other-uuid", "api"),
setupAcc: func(acc *portalsdkmocks.MockAccountAPI) {
acc.EXPECT().ListAPIKeys(context.Background(), mock.Anything).
acc.EXPECT().ListAPIKeys(mock.Anything, mock.Anything).
Return([]*portalsdk.APIKey{newTestAPIKey("my-key", uuidStr)}, 1, nil)
acc.EXPECT().DeleteAPIKey(context.Background(), uuidStr).Return(nil)
acc.EXPECT().DeleteAPIKey(mock.Anything, uuidStr).Return(nil)
},
},
{
Expand All @@ -195,7 +195,7 @@ func TestAPIKeyService_DeleteAPIKey(t *testing.T) {
force: true,
authToken: makeAPIKeyJWT(uuidStr, "api"),
setupAcc: func(acc *portalsdkmocks.MockAccountAPI) {
acc.EXPECT().DeleteAPIKey(context.Background(), uuidStr).Return(nil)
acc.EXPECT().DeleteAPIKey(mock.Anything, uuidStr).Return(nil)
},
},
{
Expand All @@ -204,7 +204,7 @@ func TestAPIKeyService_DeleteAPIKey(t *testing.T) {
force: false,
authToken: makeAPIKeyJWT("", "login"),
setupAcc: func(acc *portalsdkmocks.MockAccountAPI) {
acc.EXPECT().DeleteAPIKey(context.Background(), uuidStr).Return(nil)
acc.EXPECT().DeleteAPIKey(mock.Anything, uuidStr).Return(nil)
},
},
{
Expand All @@ -213,7 +213,7 @@ func TestAPIKeyService_DeleteAPIKey(t *testing.T) {
force: false,
authToken: "test-token",
setupAcc: func(acc *portalsdkmocks.MockAccountAPI) {
acc.EXPECT().ListAPIKeys(context.Background(), mock.Anything).
acc.EXPECT().ListAPIKeys(mock.Anything, mock.Anything).
Return([]*portalsdk.APIKey{}, 0, nil)
},
wantErr: true,
Expand All @@ -225,7 +225,7 @@ func TestAPIKeyService_DeleteAPIKey(t *testing.T) {
force: false,
authToken: "test-token",
setupAcc: func(acc *portalsdkmocks.MockAccountAPI) {
acc.EXPECT().DeleteAPIKey(context.Background(), uuidStr).
acc.EXPECT().DeleteAPIKey(mock.Anything, uuidStr).
Return(fmt.Errorf("server error"))
},
wantErr: true,
Expand Down
43 changes: 22 additions & 21 deletions pkg/cli/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/mock"
"github.com/urfave/cli/v3"
"go.lumeweb.com/pinner-cli/pkg/config"
configmocks "go.lumeweb.com/pinner-cli/pkg/config/mocks"
Expand All @@ -26,7 +27,7 @@ func TestAccountOTPEnable(t *testing.T) {
otp: "123456",
setupMocks: func(cfgMgr *configmocks.MockManager, authService *MockAuthService) {
cfgMgr.EXPECT().Config().Return(&config.Config{BaseEndpoint: "https://api.test.com"})
authService.EXPECT().EnableOTP(context.Background(), "123456").Return(nil)
authService.EXPECT().EnableOTP(mock.Anything, "123456").Return(nil)
},
wantErr: false,
},
Expand All @@ -42,7 +43,7 @@ func TestAccountOTPEnable(t *testing.T) {
otp: "000000",
setupMocks: func(cfgMgr *configmocks.MockManager, authService *MockAuthService) {
cfgMgr.EXPECT().Config().Return(&config.Config{BaseEndpoint: "https://api.test.com"})
authService.EXPECT().EnableOTP(context.Background(), "000000").
authService.EXPECT().EnableOTP(mock.Anything, "000000").
Return(errors.New("invalid OTP code"))
},
wantErr: true,
Expand Down Expand Up @@ -112,7 +113,7 @@ func TestAccountOTPDisable(t *testing.T) {
password: "password",
setupMocks: func(cfgMgr *configmocks.MockManager, authService *MockAuthService) {
cfgMgr.EXPECT().Config().Return(&config.Config{BaseEndpoint: "https://api.test.com"})
authService.EXPECT().DisableOTP(context.Background(), "password").Return(nil)
authService.EXPECT().DisableOTP(mock.Anything, "password").Return(nil)
},
wantErr: false,
},
Expand All @@ -127,7 +128,7 @@ func TestAccountOTPDisable(t *testing.T) {
password: "wrong-password",
setupMocks: func(cfgMgr *configmocks.MockManager, authService *MockAuthService) {
cfgMgr.EXPECT().Config().Return(&config.Config{BaseEndpoint: "https://api.test.com"})
authService.EXPECT().DisableOTP(context.Background(), "wrong-password").
authService.EXPECT().DisableOTP(mock.Anything, "wrong-password").
Return(errors.New("invalid password"))
},
wantErr: true,
Expand Down Expand Up @@ -196,16 +197,16 @@ func TestAuthService_EnableOTP(t *testing.T) {
name: "successful enable with provided OTP",
otp: "123456",
setupMocks: func(acc *portalsdkmocks.MockAccountAPI, prompter *MockAuthPrompter) {
acc.EXPECT().GenerateOTP(context.Background()).Return("JBSWY3DPEHPK3PXP", nil)
acc.EXPECT().VerifyOTP(context.Background(), "123456").Return(nil)
acc.EXPECT().GenerateOTP(mock.Anything).Return("JBSWY3DPEHPK3PXP", nil)
acc.EXPECT().VerifyOTP(mock.Anything, "123456").Return(nil)
},
wantErr: false,
},
{
name: "generate OTP fails",
otp: "123456",
setupMocks: func(acc *portalsdkmocks.MockAccountAPI, prompter *MockAuthPrompter) {
acc.EXPECT().GenerateOTP(context.Background()).
acc.EXPECT().GenerateOTP(mock.Anything).
Return("", errors.New("authentication required"))
},
wantErr: true,
Expand All @@ -215,8 +216,8 @@ func TestAuthService_EnableOTP(t *testing.T) {
name: "verify OTP fails",
otp: "000000",
setupMocks: func(acc *portalsdkmocks.MockAccountAPI, prompter *MockAuthPrompter) {
acc.EXPECT().GenerateOTP(context.Background()).Return("JBSWY3DPEHPK3PXP", nil)
acc.EXPECT().VerifyOTP(context.Background(), "000000").
acc.EXPECT().GenerateOTP(mock.Anything).Return("JBSWY3DPEHPK3PXP", nil)
acc.EXPECT().VerifyOTP(mock.Anything, "000000").
Return(errors.New("invalid OTP code"))
},
wantErr: true,
Expand Down Expand Up @@ -273,15 +274,15 @@ func TestAuthService_EnableOTP_Interactive(t *testing.T) {
name: "successful enable with prompted OTP",
setupMocks: func(acc *portalsdkmocks.MockAccountAPI, prompter *MockAuthPrompter) {
prompter.EXPECT().PromptOTP().Return("123456", nil)
acc.EXPECT().GenerateOTP(context.Background()).Return("JBSWY3DPEHPK3PXP", nil)
acc.EXPECT().VerifyOTP(context.Background(), "123456").Return(nil)
acc.EXPECT().GenerateOTP(mock.Anything).Return("JBSWY3DPEHPK3PXP", nil)
acc.EXPECT().VerifyOTP(mock.Anything, "123456").Return(nil)
},
wantErr: false,
},
{
name: "OTP prompt fails",
setupMocks: func(acc *portalsdkmocks.MockAccountAPI, prompter *MockAuthPrompter) {
acc.EXPECT().GenerateOTP(context.Background()).Return("JBSWY3DPEHPK3PXP", nil)
acc.EXPECT().GenerateOTP(mock.Anything).Return("JBSWY3DPEHPK3PXP", nil)
prompter.EXPECT().PromptOTP().Return("", errors.New("user cancelled"))
},
wantErr: true,
Expand Down Expand Up @@ -339,15 +340,15 @@ func TestAuthService_DisableOTP(t *testing.T) {
name: "successful disable with provided password",
password: "password",
setupMocks: func(acc *portalsdkmocks.MockAccountAPI, prompter *MockAuthPrompter) {
acc.EXPECT().DisableOTP(context.Background(), "password").Return(nil)
acc.EXPECT().DisableOTP(mock.Anything, "password").Return(nil)
},
wantErr: false,
},
{
name: "disable fails",
password: "wrong-password",
setupMocks: func(acc *portalsdkmocks.MockAccountAPI, prompter *MockAuthPrompter) {
acc.EXPECT().DisableOTP(context.Background(), "wrong-password").
acc.EXPECT().DisableOTP(mock.Anything, "wrong-password").
Return(errors.New("invalid password"))
},
wantErr: true,
Expand Down Expand Up @@ -396,7 +397,7 @@ func TestAccountOTPEnable_MockCommand_Success(t *testing.T) {
cfgMgr := newTestConfigMgr(t)
output := newTestOutput()

authService.EXPECT().EnableOTP(context.Background(), "123456").Return(nil)
authService.EXPECT().EnableOTP(mock.Anything, "123456").Return(nil)

cfgMgrFactory := func() (config.Manager, error) { return cfgMgr, nil }
authServiceFactory := func(cm config.Manager, out Output, apiEndpoint string) AuthService {
Expand All @@ -413,7 +414,7 @@ func TestAccountOTPEnable_MockCommand_NoOTP(t *testing.T) {
cfgMgr := newTestConfigMgr(t)
output := newTestOutput()

authService.EXPECT().EnableOTP(context.Background(), "").Return(nil)
authService.EXPECT().EnableOTP(mock.Anything, "").Return(nil)

cfgMgrFactory := func() (config.Manager, error) { return cfgMgr, nil }
authServiceFactory := func(cm config.Manager, out Output, apiEndpoint string) AuthService {
Expand All @@ -430,7 +431,7 @@ func TestAccountOTPEnable_MockCommand_ServiceError(t *testing.T) {
cfgMgr := newTestConfigMgr(t)
output := newTestOutput()

authService.EXPECT().EnableOTP(context.Background(), "000000").
authService.EXPECT().EnableOTP(mock.Anything, "000000").
Return(errors.New("invalid OTP code"))

cfgMgrFactory := func() (config.Manager, error) { return cfgMgr, nil }
Expand Down Expand Up @@ -460,7 +461,7 @@ func TestAccountOTPDisable_MockCommand_Success(t *testing.T) {
cfgMgr := newTestConfigMgr(t)
output := newTestOutput()

authService.EXPECT().DisableOTP(context.Background(), "mypassword").Return(nil)
authService.EXPECT().DisableOTP(mock.Anything, "mypassword").Return(nil)

cfgMgrFactory := func() (config.Manager, error) { return cfgMgr, nil }
authServiceFactory := func(cm config.Manager, out Output, apiEndpoint string) AuthService {
Expand All @@ -477,7 +478,7 @@ func TestAccountOTPDisable_MockCommand_EmptyPassword(t *testing.T) {
cfgMgr := newTestConfigMgr(t)
output := newTestOutput()

authService.EXPECT().DisableOTP(context.Background(), "").Return(nil)
authService.EXPECT().DisableOTP(mock.Anything, "").Return(nil)

cfgMgrFactory := func() (config.Manager, error) { return cfgMgr, nil }
authServiceFactory := func(cm config.Manager, out Output, apiEndpoint string) AuthService {
Expand All @@ -494,7 +495,7 @@ func TestAccountOTPDisable_MockCommand_ServiceError(t *testing.T) {
cfgMgr := newTestConfigMgr(t)
output := newTestOutput()

authService.EXPECT().DisableOTP(context.Background(), "wrong").
authService.EXPECT().DisableOTP(mock.Anything, "wrong").
Return(errors.New("invalid password"))

cfgMgrFactory := func() (config.Manager, error) { return cfgMgr, nil }
Expand Down Expand Up @@ -530,7 +531,7 @@ func TestAuthService_DisableOTP_Interactive(t *testing.T) {
name: "successful disable with prompted password",
setupMocks: func(acc *portalsdkmocks.MockAccountAPI, prompter *MockAuthPrompter) {
prompter.EXPECT().Password("Password").Return("password", nil)
acc.EXPECT().DisableOTP(context.Background(), "password").Return(nil)
acc.EXPECT().DisableOTP(mock.Anything, "password").Return(nil)
},
wantErr: false,
},
Expand Down
16 changes: 16 additions & 0 deletions pkg/cli/admin_billing_credits.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ Examples:
}

func billingCreditsListAction(ctx context.Context, cmd flagGetter, output Output, cfgMgr config.Manager, serviceFactory BillingAdminServiceFactory) error {
ctx, cancel := context.WithTimeout(ctx, cfgMgr.Config().GetDefaultTimeout())
defer cancel()
service := serviceFactory(cfgMgr, output)
if err := service.RequireAuthenticated(); err != nil {
return err
Expand Down Expand Up @@ -148,6 +150,8 @@ Examples:
}

func billingCreditsGetAction(ctx context.Context, cmd argsGetter, output Output, cfgMgr config.Manager, serviceFactory BillingAdminServiceFactory) error {
ctx, cancel := context.WithTimeout(ctx, cfgMgr.Config().GetDefaultTimeout())
defer cancel()
if cmd.Args().Len() < 1 {
return fmt.Errorf("credit ID is required")
}
Expand Down Expand Up @@ -244,6 +248,8 @@ Examples:
}

func billingCreditsCreateAction(ctx context.Context, cmd flagGetter, output Output, cfgMgr config.Manager, serviceFactory BillingAdminServiceFactory) error {
ctx, cancel := context.WithTimeout(ctx, cfgMgr.Config().GetDefaultTimeout())
defer cancel()
service := serviceFactory(cfgMgr, output)
if err := service.RequireAuthenticated(); err != nil {
return err
Expand Down Expand Up @@ -314,6 +320,8 @@ Examples:
}

func billingCreditsDeleteAction(ctx context.Context, cmd argsGetter, output Output, cfgMgr config.Manager, serviceFactory BillingAdminServiceFactory) error {
ctx, cancel := context.WithTimeout(ctx, cfgMgr.Config().GetDefaultTimeout())
defer cancel()
if cmd.Args().Len() < 1 {
return fmt.Errorf("credit ID is required")
}
Expand Down Expand Up @@ -361,6 +369,8 @@ Examples:
}

func billingCreditsRestoreAction(ctx context.Context, cmd argsGetter, output Output, cfgMgr config.Manager, serviceFactory BillingAdminServiceFactory) error {
ctx, cancel := context.WithTimeout(ctx, cfgMgr.Config().GetDefaultTimeout())
defer cancel()
if cmd.Args().Len() < 1 {
return fmt.Errorf("credit ID is required")
}
Expand Down Expand Up @@ -413,6 +423,8 @@ Examples:
}

func billingCreditsPurgeAction(ctx context.Context, cmd flagGetter, output Output, cfgMgr config.Manager, serviceFactory BillingAdminServiceFactory) error {
ctx, cancel := context.WithTimeout(ctx, cfgMgr.Config().GetDefaultTimeout())
defer cancel()
service := serviceFactory(cfgMgr, output)
if err := service.RequireAuthenticated(); err != nil {
return err
Expand Down Expand Up @@ -459,6 +471,8 @@ Examples:
}

func billingCreditsUserBalanceAction(ctx context.Context, cmd argsGetter, output Output, cfgMgr config.Manager, serviceFactory BillingAdminServiceFactory) error {
ctx, cancel := context.WithTimeout(ctx, cfgMgr.Config().GetDefaultTimeout())
defer cancel()
if cmd.Args().Len() < 1 {
return fmt.Errorf("user ID is required")
}
Expand Down Expand Up @@ -519,6 +533,8 @@ Examples:
}

func billingCreditsUserDeletedCreditsAction(ctx context.Context, cmd argsFlagGetter, output Output, cfgMgr config.Manager, serviceFactory BillingAdminServiceFactory) error {
ctx, cancel := context.WithTimeout(ctx, cfgMgr.Config().GetDefaultTimeout())
defer cancel()
if cmd.Args().Len() < 1 {
return fmt.Errorf("user ID is required")
}
Expand Down
Loading
Loading