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

216 lines
5.1 KiB
Go

package cli
import (
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestHandleGetSecrets(t *testing.T) {
tests := []struct {
name string
runOutput []byte
runErr error
expectedArgs []string
expectedCode int
expectedBody []AppSecret
}{
{
name: "success no secrets",
runOutput: []byte(`[]`),
expectedArgs: []string{"app", "secret", "list", "myapp", "-m"},
expectedCode: http.StatusOK,
expectedBody: []AppSecret{},
},
{
name: "success with secrets",
runOutput: []byte(`[
{
"created on server": "true",
"generated name": "myapp_secret",
"name": "secret",
"version": "v1"
},
{
"created on server": "false",
"generated name": "myapp_unknown",
"name": "unknown",
"version": "v1"
}
]`),
expectedArgs: []string{"app", "secret", "list", "myapp", "-m"},
expectedCode: http.StatusOK,
expectedBody: []AppSecret{
{
Name: "secret",
Version: "v1",
Value: "",
Created: true},
{
Name: "unknown",
Version: "v1",
Value: "",
Created: false},
},
},
{
name: "failure bad json",
runOutput: []byte(`
[
{
"created on server": "true",
}
]`),
expectedArgs: []string{"app", "secret", "list", "myapp", "-m"},
expectedCode: http.StatusInternalServerError,
},
{
name: "failure missing fields",
runOutput: []byte(`
[
{
"created on server": "true",
"generated name": "myapp_fail"
}
]`),
expectedArgs: []string{"app", "secret", "list", "myapp", "-m"},
expectedCode: http.StatusInternalServerError,
},
{
name: "failure null field",
runOutput: []byte(`
[
{
"created on server": null,
"generated name": "myapp_nil",
"name": "nil",
"version": "v1"
}
]`),
expectedArgs: []string{"app", "secret", "list", "myapp", "-m"},
expectedCode: http.StatusInternalServerError,
},
{
name: "command failure",
runOutput: []byte("error"),
runErr: errors.New("failed"),
expectedArgs: []string{"app", "secret", "list", "myapp", "-m"},
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.handleGetAppSecrets(rec, req, "myapp")
assert.Equal(t, tt.expectedCode, rec.Code)
if tt.expectedCode == http.StatusOK {
var res []AppSecret
err := json.Unmarshal(rec.Body.Bytes(), &res)
require.NoError(t, err)
assert.Equal(t, tt.expectedBody, res)
}
mockCmd.AssertExpectations(t)
})
}
}
func TestHandleInsert(t *testing.T) {
tests := []struct {
name string
reqBody string
runOutput []byte
runErr error
expectedArgs []string
expectedRun bool
expectedCode int
}{
{
name: "success",
reqBody: `{
"name": "myapp_secret",
"version": "v1",
"value": "secret"
}`,
runOutput: []byte(nil),
expectedArgs: []string{"app", "secret", "insert", "myapp", "myapp_secret", "v1", "secret"},
expectedRun: true,
expectedCode: http.StatusOK,
},
{
name: "failure missing fields",
reqBody: `{
"name": "myapp_secret",
}`,
runOutput: []byte(nil),
expectedArgs: []string{"app", "secret", "insert", "myapp", "myapp_secret", "v1", "secret"},
expectedRun: false,
expectedCode: http.StatusBadRequest,
},
{
name: "failure abra",
reqBody: `{
"name": "myapp_secret",
"version": "v1",
"value": "secret"
}`,
runOutput: []byte(nil),
runErr: errors.New("error"),
expectedArgs: []string{"app", "secret", "insert", "myapp", "myapp_secret", "v1", "secret"},
expectedRun: true,
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, "/", strings.NewReader(tt.reqBody))
req.Header.Set("Content-Type", "application/json")
rec := httptest.NewRecorder()
h.handleInsertAppSecret(rec, req, "myapp")
assert.Equal(t, tt.expectedCode, rec.Code)
if tt.expectedRun {
mockCmd.AssertExpectations(t)
}
})
}
}