-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add integration tests in Go for important commands
- Loading branch information
1 parent
d4f0f09
commit 276fb05
Showing
29 changed files
with
1,380 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,3 +56,6 @@ jobs: | |
uses: golangci/golangci-lint-action@v3 | ||
with: | ||
version: v1.59 | ||
|
||
- name: Run tests | ||
run: make test |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
// Package mockapi provides mocks of the HTTP API for use in integration tests. | ||
package mockapi | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/go-chi/chi/v5/middleware" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
type Handler struct { | ||
*chi.Mux | ||
|
||
t *testing.T | ||
|
||
store | ||
} | ||
|
||
func NewHandler(t *testing.T) *Handler { | ||
h := &Handler{t: t} | ||
h.Mux = chi.NewRouter() | ||
|
||
if testing.Verbose() { | ||
h.Mux.Use(middleware.DefaultLogger) | ||
} | ||
|
||
h.Mux.Use(func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
authHeader := req.Header.Get("Authorization") | ||
require.NotEmpty(t, authHeader) | ||
require.True(t, strings.HasPrefix(authHeader, "Bearer ")) | ||
next.ServeHTTP(w, req) | ||
}) | ||
}) | ||
|
||
h.Mux.Get("/users/me", h.handleUsersMe) | ||
h.Mux.Get("/users/{id}/extended-access", h.handleUserExtendedAccess) | ||
h.Mux.Get("/ref/users", h.handleUserRefs) | ||
h.Mux.Post("/me/verification", func(w http.ResponseWriter, _ *http.Request) { | ||
_ = json.NewEncoder(w).Encode(map[string]any{"state": false, "type": ""}) | ||
}) | ||
|
||
h.Mux.Get("/organizations", h.handleListOrgs) | ||
h.Mux.Get("/organizations/{id}", h.handleGetOrg) | ||
h.Mux.Get("/users/{id}/organizations", h.handleListOrgs) | ||
h.Mux.Get("/ref/organizations", h.handleOrgRefs) | ||
|
||
h.Mux.Post("/organizations/{id}/subscriptions", h.handleCreateSubscription) | ||
h.Mux.Get("/subscriptions/{id}", h.handleGetSubscription) | ||
h.Mux.Get("/organizations/{id}/setup/options", func(w http.ResponseWriter, _ *http.Request) { | ||
type options struct { | ||
Plans []string `json:"plans"` | ||
Regions []string `json:"regions"` | ||
} | ||
_ = json.NewEncoder(w).Encode(options{[]string{"development"}, []string{"test-region"}}) | ||
}) | ||
h.Mux.Get("/organizations/{id}/subscriptions/estimate", func(w http.ResponseWriter, _ *http.Request) { | ||
_ = json.NewEncoder(w).Encode(map[string]any{"total": "$1,000 USD"}) | ||
}) | ||
|
||
h.Mux.Get("/projects/{id}", h.handleGetProject) | ||
h.Mux.Get("/projects/{id}/environments", h.handleListEnvironments) | ||
h.Mux.Get("/projects/{project_id}/environments/{environment_id}/deployments/current", h.handleGetCurrentDeployment) | ||
h.Mux.Get("/ref/projects", h.handleProjectRefs) | ||
|
||
h.Mux.Get("/regions", h.handleListRegions) | ||
|
||
return h | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
package mockapi | ||
|
||
import ( | ||
"crypto/ed25519" | ||
"crypto/rand" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"slices" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
var ValidAPITokens = []string{"api-token-1"} | ||
var accessTokens = []string{"access-token-1"} | ||
|
||
// NewAuthServer creates a new mock authentication server. | ||
// The caller must call Close() on the server when finished. | ||
func NewAuthServer(t *testing.T) *httptest.Server { | ||
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) { | ||
if testing.Verbose() { | ||
t.Log(req) | ||
} | ||
if req.Method == http.MethodPost && req.URL.Path == "/oauth2/token" { | ||
require.NoError(t, req.ParseForm()) | ||
if gt := req.Form.Get("grant_type"); gt != "api_token" { | ||
w.WriteHeader(http.StatusBadRequest) | ||
_ = json.NewEncoder(w).Encode(map[string]string{"error": "invalid grant type: " + gt}) | ||
return | ||
} | ||
apiToken := req.Form.Get("api_token") | ||
if slices.Contains(ValidAPITokens, apiToken) { | ||
_ = json.NewEncoder(w).Encode(struct { | ||
AccessToken string `json:"access_token"` | ||
ExpiresIn int `json:"expires_in"` | ||
Type string `json:"token_type"` | ||
}{AccessToken: accessTokens[0], ExpiresIn: 60, Type: "bearer"}) | ||
return | ||
} | ||
w.WriteHeader(http.StatusBadRequest) | ||
_ = json.NewEncoder(w).Encode(map[string]string{"error": "invalid API token"}) | ||
return | ||
} | ||
|
||
if req.Method == http.MethodPost && req.URL.Path == "/ssh" { | ||
var options struct { | ||
PublicKey string `json:"key"` | ||
} | ||
err := json.NewDecoder(req.Body).Decode(&options) | ||
require.NoError(t, err) | ||
key, _, _, _, err := ssh.ParseAuthorizedKey([]byte(options.PublicKey)) | ||
require.NoError(t, err) | ||
signer, err := sshSigner() | ||
require.NoError(t, err) | ||
extensions := make(map[string]string) | ||
|
||
// Add standard ssh options | ||
extensions["permit-X11-forwarding"] = "" | ||
extensions["permit-agent-forwarding"] = "" | ||
extensions["permit-port-forwarding"] = "" | ||
extensions["permit-pty"] = "" | ||
extensions["permit-user-rc"] = "" | ||
cert := &ssh.Certificate{ | ||
Key: key, | ||
Serial: 0, | ||
CertType: ssh.UserCert, | ||
KeyId: "test-key-id", | ||
ValidAfter: uint64(time.Now().Add(-1 * time.Second).Unix()), | ||
ValidBefore: uint64(time.Now().Add(time.Minute).Unix()), | ||
Permissions: ssh.Permissions{ | ||
Extensions: extensions, | ||
}, | ||
} | ||
err = cert.SignCert(rand.Reader, signer) | ||
require.NoError(t, err) | ||
_ = json.NewEncoder(w).Encode(struct { | ||
Cert string `json:"certificate"` | ||
}{string(ssh.MarshalAuthorizedKey(cert))}) | ||
require.NoError(t, err) | ||
return | ||
} | ||
|
||
w.WriteHeader(http.StatusNotFound) | ||
_ = json.NewEncoder(w).Encode(map[string]string{"error": "not found"}) | ||
})) | ||
} | ||
|
||
var signer ssh.Signer // TODO reuse to validate SSH connection | ||
|
||
func sshSigner() (ssh.Signer, error) { | ||
if signer != nil { | ||
return signer, nil | ||
} | ||
_, privateKey, err := ed25519.GenerateKey(rand.Reader) | ||
if err != nil { | ||
return nil, err | ||
} | ||
s, err := ssh.NewSignerFromKey(privateKey) | ||
if err != nil { | ||
return nil, err | ||
} | ||
signer = s | ||
return s, nil | ||
} |
Oops, something went wrong.