func TestAuthentication(t *testing.T) {
	m := &Authentication{
		findAccessToken: func(token string) (*empire.AccessToken, error) {
			return &empire.AccessToken{
				User: &empire.User{
					Name: "ehjolmes",
				},
			}, nil
		},
		handler: httpx.HandlerFunc(func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
			_, ok := empire.UserFromContext(ctx)
			if !ok {
				t.Fatal("Expected a user to be present in the context")
			}

			return nil
		}),
	}

	ctx := context.Background()
	resp := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/apps", nil)
	req.SetBasicAuth("", "token")

	if err := m.ServeHTTPContext(ctx, resp, req); err != nil {
		t.Fatal(err)
	}
}
Exemple #2
0
func newDeploymentsCreateOpts(ctx context.Context, w http.ResponseWriter, req *http.Request) (*empire.DeploymentsCreateOpts, error) {
	var form PostDeployForm

	if err := Decode(req, &form); err != nil {
		return nil, err
	}

	w.Header().Set("Content-Type", "application/json; boundary=NL")

	if form.Image.Tag == "" && form.Image.Digest == "" {
		form.Image.Tag = "latest"
	}

	user, _ := empire.UserFromContext(ctx)

	opts := empire.DeploymentsCreateOpts{
		Image:  form.Image,
		Output: streamhttp.StreamingResponseWriter(w),
		User:   user,
	}
	return &opts, nil
}