diff --git a/pkg/cmd/build/vulcan.go b/pkg/cmd/build/vulcan.go index a576d21a5..2406d336e 100644 --- a/pkg/cmd/build/vulcan.go +++ b/pkg/cmd/build/vulcan.go @@ -7,7 +7,7 @@ import ( msg "github.com/aziontech/azion-cli/messages/build" "github.com/aziontech/azion-cli/pkg/contracts" "github.com/aziontech/azion-cli/pkg/logger" - vul "github.com/aziontech/azion-cli/pkg/vulcan" + vulcanPkg "github.com/aziontech/azion-cli/pkg/vulcan" "github.com/aziontech/azion-cli/utils" "go.uber.org/zap" ) @@ -19,6 +19,7 @@ func vulcan(cmd *BuildCmd, conf *contracts.AzionApplicationOptions, vulcanParams return err } + vul := vulcanPkg.NewVulcan() err = vul.CheckVulcanMajor(vulcanVer, cmd.f) if err != nil { return err diff --git a/pkg/cmd/dev/vulcan.go b/pkg/cmd/dev/vulcan.go index 9be607358..3c7849061 100644 --- a/pkg/cmd/dev/vulcan.go +++ b/pkg/cmd/dev/vulcan.go @@ -5,12 +5,13 @@ import ( msg "github.com/aziontech/azion-cli/messages/dev" "github.com/aziontech/azion-cli/pkg/logger" - vul "github.com/aziontech/azion-cli/pkg/vulcan" + vulcanPkg "github.com/aziontech/azion-cli/pkg/vulcan" "go.uber.org/zap" ) func vulcan(cmd *DevCmd, isFirewall bool) error { + vul := vulcanPkg.NewVulcan() command := vul.Command("", "dev", cmd.F) if isFirewall { command = vul.Command("", "dev --firewall", cmd.F) diff --git a/pkg/cmd/init/utils.go b/pkg/cmd/init/utils.go index 8e38d568a..7a2f77a9d 100644 --- a/pkg/cmd/init/utils.go +++ b/pkg/cmd/init/utils.go @@ -8,7 +8,7 @@ import ( "github.com/AlecAivazis/survey/v2" msg "github.com/aziontech/azion-cli/messages/init" "github.com/aziontech/azion-cli/pkg/logger" - vul "github.com/aziontech/azion-cli/pkg/vulcan" + vulcanPkg "github.com/aziontech/azion-cli/pkg/vulcan" helpers "github.com/aziontech/azion-cli/utils" "github.com/joho/godotenv" "go.uber.org/zap" @@ -41,6 +41,7 @@ func (cmd *initCmd) selectVulcanTemplates() error { return err } + vul := vulcanPkg.NewVulcan() err = vul.CheckVulcanMajor(vulcanVer, cmd.f) if err != nil { return err diff --git a/pkg/cmd/link/utils.go b/pkg/cmd/link/utils.go index 3cead854c..b42b1e138 100644 --- a/pkg/cmd/link/utils.go +++ b/pkg/cmd/link/utils.go @@ -7,8 +7,7 @@ import ( "github.com/AlecAivazis/survey/v2" msg "github.com/aziontech/azion-cli/messages/init" "github.com/aziontech/azion-cli/pkg/logger" - "github.com/aziontech/azion-cli/pkg/vulcan" - vul "github.com/aziontech/azion-cli/pkg/vulcan" + vulcanPkg "github.com/aziontech/azion-cli/pkg/vulcan" helpers "github.com/aziontech/azion-cli/utils" "go.uber.org/zap" ) @@ -73,7 +72,8 @@ func (cmd *LinkCmd) selectVulcanMode(info *LinkInfo) error { return err } - err = vulcan.CheckVulcanMajor(vulcanVer, cmd.F) + vul := vulcanPkg.NewVulcan() + err = vul.CheckVulcanMajor(vulcanVer, cmd.F) if err != nil { return err } diff --git a/pkg/token/fixtures/response.json b/pkg/token/fixtures/response.json new file mode 100644 index 000000000..9c7539204 --- /dev/null +++ b/pkg/token/fixtures/response.json @@ -0,0 +1,3 @@ +{ + "Token": "123321" +} \ No newline at end of file diff --git a/pkg/token/token_test.go b/pkg/token/token_test.go index aaa52bd15..eb6436625 100644 --- a/pkg/token/token_test.go +++ b/pkg/token/token_test.go @@ -3,6 +3,7 @@ package token import ( "net/http" "os" + "path/filepath" "testing" "github.com/aziontech/azion-cli/pkg/config" @@ -96,3 +97,114 @@ func Test_Save(t *testing.T) { } }) } + +func Test_Create(t *testing.T) { + logger.New(zapcore.DebugLevel) + + t.Run("create token", func(t *testing.T) { + mock := &httpmock.Registry{} + mock.Register( + httpmock.REST("POST", "token/tokens"), + httpmock.JSONFromFile("./fixtures/response.json"), + ) + + token, err := New(&Config{ + Client: &http.Client{Transport: mock}, + Out: os.Stdout, + }) + if err != nil { + t.Fatalf("NewToken() = %v; want nil", err) + } + + token.endpoint = "http://api.azion.net/token" + response, err := token.Create("base64Credentials") + if err != nil { + t.Fatalf("Create() = %v; want nil", err) + } + + if response.Token != "123321" { + t.Errorf("Create() = %v; want 123321", response.Token) + } + }) +} + +func Test_WriteSettings(t *testing.T) { + logger.New(zapcore.DebugLevel) + + t.Run("write settings to disk", func(t *testing.T) { + settings := Settings{ + Token: "tokenValue", + UUID: "uuidValue", + } + + errSetPath := config.SetPath("/tmp/testazion/test.toml") + if errSetPath != nil { + t.Fatalf("SetPath() error: %s;", errSetPath.Error()) + } + + err := WriteSettings(settings) + if err != nil { + t.Fatalf("WriteSettings() = %v; want nil", err) + } + + dir, err := config.Dir() + require.NoError(t, err) + + data, err := os.ReadFile(filepath.Join(dir.Dir, dir.Settings)) + require.NoError(t, err) + + var readSettings Settings + err = toml.Unmarshal(data, &readSettings) + require.NoError(t, err) + + if readSettings.Token != settings.Token || readSettings.UUID != settings.UUID { + t.Errorf("WriteSettings() wrote %v; want %v", readSettings, settings) + } + }) +} + +func Test_ReadSettings(t *testing.T) { + logger.New(zapcore.DebugLevel) + + t.Run("read settings from disk", func(t *testing.T) { + errSetPath := config.SetPath("/tmp/testazion/test.toml") + if errSetPath != nil { + t.Fatalf("SetPath() error: %s;", errSetPath.Error()) + } + + expectedSettings := Settings{ + Token: "tokenValue", + UUID: "uuidValue", + } + + err := WriteSettings(expectedSettings) + if err != nil { + t.Fatalf("WriteSettings() error: %s", err) + } + + settings, err := ReadSettings() + if err != nil { + t.Fatalf("ReadSettings() = %v; want nil", err) + } + + if settings.Token != expectedSettings.Token || settings.UUID != expectedSettings.UUID { + t.Errorf("ReadSettings() read %v; want %v", settings, expectedSettings) + } + }) + + t.Run("read settings from non-existing file", func(t *testing.T) { + errSetPath := config.SetPath("/tmp/testazion/nonexistent.toml") + if errSetPath != nil { + t.Fatalf("SetPath() error: %s;", errSetPath.Error()) + } + + settings, err := ReadSettings() + if err == nil { + t.Fatalf("ReadSettings() error = nil; want non-nil error") + } + + if settings != (Settings{}) { + t.Errorf("ReadSettings() = %v; want empty settings", settings) + } + }) +} diff --git a/pkg/vulcan/vulcan.go b/pkg/vulcan/vulcan.go index d4c940357..bd1a8c498 100644 --- a/pkg/vulcan/vulcan.go +++ b/pkg/vulcan/vulcan.go @@ -21,7 +21,19 @@ const ( var versionVulcan = "@latest" -func Command(flags, params string, f *cmdutil.Factory) string { +type VulcanPkg struct { + Command func(flags, params string, f *cmdutil.Factory) string + CheckVulcanMajor func(currentVersion string, f *cmdutil.Factory) error +} + +func NewVulcan() *VulcanPkg { + return &VulcanPkg{ + Command: command, + CheckVulcanMajor: checkVulcanMajor, + } +} + +func command(flags, params string, f *cmdutil.Factory) string { if f.Logger.Debug { installDebug := "DEBUG=true " + installEdgeFunctions return fmt.Sprintf(installDebug, flags, versionVulcan, params) @@ -29,7 +41,7 @@ func Command(flags, params string, f *cmdutil.Factory) string { return fmt.Sprintf(installEdgeFunctions, flags, versionVulcan, params) } -func CheckVulcanMajor(currentVersion string, f *cmdutil.Factory) error { +func checkVulcanMajor(currentVersion string, f *cmdutil.Factory) error { parts := strings.Split(currentVersion, ".") // Extract the first part and convert it to a number diff --git a/pkg/vulcan/vulcan_test.go b/pkg/vulcan/vulcan_test.go index 648254090..92a997b07 100644 --- a/pkg/vulcan/vulcan_test.go +++ b/pkg/vulcan/vulcan_test.go @@ -42,8 +42,9 @@ func TestCommand(t *testing.T) { } f, _, _ := testutils.NewFactory(nil) for _, tt := range tests { + vul := NewVulcan() t.Run(tt.name, func(t *testing.T) { - if got := Command(tt.args.flags, tt.args.params, f); got != tt.want { + if got := vul.Command(tt.args.flags, tt.args.params, f); got != tt.want { t.Errorf("Command() = %v, want %v", got, tt.want) } })