124 lines
3.0 KiB
Go
124 lines
3.0 KiB
Go
package cli
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/mock"
|
|
)
|
|
|
|
type MockSecretUtils struct {
|
|
mock.Mock
|
|
}
|
|
|
|
func (su *MockSecretUtils) createSecrets(appName string, domain string, server string) (map[string]string, error) {
|
|
args := su.Called(appName, domain, server)
|
|
return args.Get(0).(map[string]string), args.Error(1)
|
|
|
|
}
|
|
func TestHandleNewApp(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
reqBody string
|
|
runOutput []byte
|
|
runErr error
|
|
runSecrets bool
|
|
expectedSecretArgs []string
|
|
secretOutput map[string]string
|
|
secretErr error
|
|
expectedArgs []string
|
|
expectedRun bool
|
|
expectedCode int
|
|
}{
|
|
{
|
|
name: "success no chaos no secrets",
|
|
reqBody: `{
|
|
"domain": "app.xyz",
|
|
"server": "server.xyz",
|
|
"chaos": false,
|
|
"secrets": false
|
|
}`,
|
|
runOutput: []byte(nil),
|
|
expectedArgs: []string{"app", "new", "myapp", "-n", "--domain=app.xyz", "--server=server.xyz"},
|
|
expectedRun: true,
|
|
expectedCode: http.StatusOK,
|
|
},
|
|
{
|
|
name: "success with chaos no secrets",
|
|
reqBody: `{
|
|
"domain": "app.xyz",
|
|
"server": "server.xyz",
|
|
"chaos": true,
|
|
"secrets": false
|
|
}`,
|
|
runOutput: []byte(nil),
|
|
expectedArgs: []string{"app", "new", "myapp", "-n", "--domain=app.xyz", "--server=server.xyz", "-C"},
|
|
expectedRun: true,
|
|
expectedCode: http.StatusOK,
|
|
},
|
|
{
|
|
name: "success no chaos with secrets",
|
|
reqBody: `{
|
|
"domain": "app.xyz",
|
|
"server": "server.xyz",
|
|
"chaos": false,
|
|
"secrets": true
|
|
}`,
|
|
runSecrets: true,
|
|
expectedSecretArgs: []string{"myapp", "app.xyz", "server.xyz"},
|
|
secretOutput: map[string]string{
|
|
"name": "secret",
|
|
"value": "secret-value",
|
|
},
|
|
runOutput: []byte(nil),
|
|
expectedArgs: []string{"app", "new", "myapp", "-n", "--domain=app.xyz", "--server=server.xyz"},
|
|
expectedRun: true,
|
|
expectedCode: http.StatusOK,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
mockCmd := new(MockCommandRunner)
|
|
mockSecrets := new(MockSecretUtils)
|
|
|
|
args := []interface{}{"abra"}
|
|
for _, arg := range tt.expectedArgs {
|
|
args = append(args, arg)
|
|
}
|
|
secretArgs := []interface{}{}
|
|
for _, arg := range tt.expectedSecretArgs {
|
|
secretArgs = append(secretArgs, arg)
|
|
}
|
|
mockCmd.
|
|
On("Run", args...).
|
|
Return(tt.runOutput, tt.runErr)
|
|
|
|
if tt.runSecrets {
|
|
mockSecrets.
|
|
On("createSecrets", secretArgs...).
|
|
Return(tt.secretOutput, tt.secretErr)
|
|
}
|
|
|
|
h := &abraHandler{cmd: mockCmd, secret: mockSecrets}
|
|
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(tt.reqBody))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
rec := httptest.NewRecorder()
|
|
|
|
h.handleNewApp(rec, req, "myapp")
|
|
|
|
assert.Equal(t, tt.expectedCode, rec.Code)
|
|
|
|
if tt.expectedRun {
|
|
mockCmd.AssertExpectations(t)
|
|
}
|
|
if tt.runSecrets {
|
|
mockSecrets.AssertExpectations(t)
|
|
}
|
|
})
|
|
}
|
|
}
|