Skip to content

Commit

Permalink
tests: add unit tests for the token package
Browse files Browse the repository at this point in the history
  • Loading branch information
PatrickMenoti committed Jul 11, 2024
1 parent b791f05 commit 8d78ef1
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 9 deletions.
3 changes: 2 additions & 1 deletion pkg/cmd/build/vulcan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/dev/vulcan.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion pkg/cmd/init/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/link/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}
Expand Down
3 changes: 3 additions & 0 deletions pkg/token/fixtures/response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"Token": "123321"
}
112 changes: 112 additions & 0 deletions pkg/token/token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package token
import (
"net/http"
"os"
"path/filepath"
"testing"

"github.com/aziontech/azion-cli/pkg/config"
Expand Down Expand Up @@ -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)
}
})
}
16 changes: 14 additions & 2 deletions pkg/vulcan/vulcan.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,27 @@ 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)
}
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
Expand Down
3 changes: 2 additions & 1 deletion pkg/vulcan/vulcan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
})
Expand Down

0 comments on commit 8d78ef1

Please sign in to comment.