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
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package activateserviceaccount
import (
"testing"

"github.com/stackitcloud/stackit-cli/internal/pkg/auth"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/zalando/go-keyring"

"github.com/google/go-cmp/cmp"
)
Expand Down Expand Up @@ -120,3 +122,62 @@ func TestParseInput(t *testing.T) {
})
}
}

func TestStoreFlags(t *testing.T) {
tests := []struct {
description string
model *inputModel
isValid bool
}{
{
description: "base",
model: fixtureInputModel(),
isValid: true,
},
{
description: "no values",
model: &inputModel{
ServiceAccountToken: "",
ServiceAccountKeyPath: "",
PrivateKeyPath: "",
TokenCustomEndpoint: "",
JwksCustomEndpoint: "",
},
isValid: true,
},
}

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
// Initialize an empty keyring
keyring.MockInit()

err := storeFlags(tt.model)
if !tt.isValid {
if err == nil {
t.Fatalf("did not fail on invalid input")
}
return
}
if err != nil {
t.Fatalf("store flags: %v", err)
}

value, err := auth.GetAuthField(auth.TOKEN_CUSTOM_ENDPOINT)
Comment thread
vicentepinto98 marked this conversation as resolved.
if err != nil {
t.Errorf("Failed to get value of auth field: %v", err)
}
if value != tt.model.TokenCustomEndpoint {
t.Errorf("Value of \"%s\" does not match: expected \"%s\", got \"%s\"", auth.TOKEN_CUSTOM_ENDPOINT, tt.model.TokenCustomEndpoint, value)
}

value, err = auth.GetAuthField(auth.JWKS_CUSTOM_ENDPOINT)
if err != nil {
t.Errorf("Failed to get value of auth field: %v", err)
}
if value != tt.model.JwksCustomEndpoint {
t.Errorf("Value of \"%s\" does not match: expected \"%s\", got \"%s\"", auth.JWKS_CUSTOM_ENDPOINT, tt.model.TokenCustomEndpoint, value)
}
})
}
}
75 changes: 75 additions & 0 deletions internal/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package cmd

import (
"errors"
"testing"

"github.com/spf13/cobra"
pkgErrors "github.com/stackitcloud/stackit-cli/internal/pkg/errors"
)

var cmd *cobra.Command
var service *cobra.Command
var resource *cobra.Command
var operation *cobra.Command

func setupCmd() {
cmd = &cobra.Command{
Use: "stackit",
}
service = &cobra.Command{
Use: "service",
}
resource = &cobra.Command{
Use: "resource",
}
operation = &cobra.Command{
Use: "operation",
}
cmd.AddCommand(service)
service.AddCommand(resource)
resource.AddCommand(operation)
}

func TestBeautifyUnknownAndMissingCommandsError(t *testing.T) {
tests := []struct {
description string
inputError error
command *cobra.Command
expectedMsg string
isNotUnknownFlagError bool
}{
{
description: "root command, extra input is a flag",
inputError: errors.New("unknown flag: --something"),
command: cmd,
expectedMsg: pkgErrors.SUBCOMMAND_MISSING,
},
{
description: "non unknown flag error, return the same",
inputError: errors.New("some error"),
command: cmd,
expectedMsg: "some error",
isNotUnknownFlagError: true,
},
}

setupCmd()
for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
actualError := beautifyUnknownAndMissingCommandsError(cmd, tt.inputError)

if tt.isNotUnknownFlagError {
if actualError.Error() != tt.expectedMsg {
t.Fatalf("expected error message to be %s, got %s", tt.expectedMsg, actualError.Error())
}
return
}

appendedErr := pkgErrors.AppendUsageTip(errors.New(tt.expectedMsg), cmd)
if actualError.Error() != appendedErr.Error() {
t.Fatalf("expected error to be %s, got %s", appendedErr.Error(), actualError.Error())
Comment thread
DiogoFerrao marked this conversation as resolved.
}
})
}
}
71 changes: 71 additions & 0 deletions internal/pkg/config/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package config

import (
"os"
"path/filepath"
"testing"

"github.com/spf13/viper"
)

func TestWrite(t *testing.T) {
tests := []struct {
description string
folderName string
folderExists bool
}{
{
description: "write config file",
folderName: "",
},
{
description: "write config file to new folder",
folderName: "new-folder",
},
{
description: "write config file to existing folder",
folderName: "existing-folder",
folderExists: true,
},
}

for _, tt := range tests {
t.Run(tt.description, func(t *testing.T) {
configPath := filepath.Join(os.TempDir(), tt.folderName, "config.json")
viper.SetConfigFile(configPath)
folderPath = filepath.Dir(configPath)

if tt.folderExists {
err := os.MkdirAll(folderPath, os.ModePerm)
if err != nil {
t.Fatalf("expected error to be nil, got %v", err)
}
}

err := Write()
if err != nil {
t.Fatalf("expected error to be nil, got %v", err)
}

// Check if the file was created
_, err = os.Stat(configPath)
if os.IsNotExist(err) {
t.Fatalf("expected file to exist, got %v", err)
}

// Delete the file
Comment thread
DiogoFerrao marked this conversation as resolved.
err = os.Remove(configPath)
if err != nil {
t.Fatalf("expected error to be nil, got %v", err)
}

// Delete the folder
if tt.folderName != "" {
err = os.Remove(folderPath)
if err != nil {
t.Fatalf("expected error to be nil, got %v", err)
}
}
})
}
}
Loading