Files
2026-09-03 21:57:42 -04:00

169 lines
3.8 KiB
Go

package cli
import (
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestHandleUpgradeApp(t *testing.T) {
tests := []struct {
name string
force bool
runOutput []byte
runErr error
expectedArgs []string
expectedCode int
}{
{
name: "success",
force: false,
runOutput: []byte("ok"),
expectedArgs: []string{"app", "upgrade", "myapp", "-n", "-c"},
expectedCode: http.StatusOK,
},
{
name: "success with force",
force: true,
runOutput: []byte("ok"),
expectedArgs: []string{"app", "upgrade", "myapp", "-n", "-c", "-f"},
expectedCode: http.StatusOK,
},
{
name: "command failure",
force: false,
runOutput: []byte("error"),
runErr: errors.New("failed"),
expectedArgs: []string{"app", "upgrade", "myapp", "-n", "-c"},
expectedCode: http.StatusInternalServerError,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockCmd := new(MockCommandRunner)
args := []interface{}{"abra"}
for _, arg := range tt.expectedArgs {
args = append(args, arg)
}
mockCmd.
On("Run", args...).
Return(tt.runOutput, tt.runErr)
h := &abraHandler{cmd: mockCmd}
req := httptest.NewRequest(http.MethodPost, "/", nil)
rec := httptest.NewRecorder()
h.handleUpgradeApp(rec, req, "myapp", tt.force)
assert.Equal(t, tt.expectedCode, rec.Code)
mockCmd.AssertExpectations(t)
})
}
}
func TestHandleRollbackApp(t *testing.T) {
tests := []struct {
name string
body string
force bool
runOutput []byte
runErr error
expectedArgs []string
expectedCode int
expectedRun bool
}{
{
name: "success with auto version no force",
body: `{
"version": "Auto"
}`,
force: false,
runOutput: []byte("ok"),
expectedArgs: []string{"app", "rollback", "myapp", "-n", "-c"},
expectedCode: http.StatusOK,
expectedRun: true,
},
{
name: "success with auto version with force",
body: `{
"version": "Auto"
}`,
force: true,
runOutput: []byte("ok"),
expectedArgs: []string{"app", "rollback", "myapp", "-n", "-c", "-f"},
expectedCode: http.StatusOK,
expectedRun: true,
},
{
name: "failure no body",
body: `{
}`,
force: false,
runOutput: []byte("ok"),
expectedArgs: []string{"app", "rollback", "myapp", "-n", "-c"},
expectedCode: http.StatusBadRequest,
expectedRun: false,
},
{
name: "failure unknown field",
body: `{
"unknown": "unknown"
}`,
force: false,
expectedArgs: []string{"app", "rollback", "myapp", "-n", "-c"},
expectedCode: http.StatusBadRequest,
expectedRun: false,
},
{
name: "failure abra error",
body: `{
"version": "1.0.0"
}`,
force: false,
runOutput: []byte("error"),
runErr: errors.New("failed"),
expectedArgs: []string{"app", "rollback", "myapp", "-n", "-c", "1.0.0"},
expectedCode: http.StatusInternalServerError,
expectedRun: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockCmd := new(MockCommandRunner)
args := []interface{}{"abra"}
for _, arg := range tt.expectedArgs {
args = append(args, arg)
}
if tt.expectedRun {
mockCmd.
On("Run", args...).
Return(tt.runOutput, tt.runErr).
Once()
}
h := &abraHandler{cmd: mockCmd}
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(tt.body))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
h.handleRollbackApp(rec, req, "myapp", tt.force)
assert.Equal(t, tt.expectedCode, rec.Code)
mockCmd.AssertExpectations(t)
})
}
}