2022-02-01 22:10:11 +00:00
|
|
|
package httpmw_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
"net/http/httptest"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2023-08-18 18:55:43 +00:00
|
|
|
"github.com/coder/coder/v2/coderd/database"
|
|
|
|
"github.com/coder/coder/v2/coderd/database/dbgen"
|
2023-10-30 17:42:20 +00:00
|
|
|
"github.com/coder/coder/v2/coderd/database/dbmem"
|
2023-08-18 18:55:43 +00:00
|
|
|
"github.com/coder/coder/v2/coderd/httpmw"
|
|
|
|
"github.com/coder/coder/v2/codersdk"
|
2022-02-01 22:10:11 +00:00
|
|
|
)
|
|
|
|
|
2022-04-06 17:42:40 +00:00
|
|
|
func TestTemplateVersionParam(t *testing.T) {
|
2022-02-01 22:10:11 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2022-04-06 17:42:40 +00:00
|
|
|
setupAuthentication := func(db database.Store) (*http.Request, database.Template) {
|
2023-02-08 19:47:05 +00:00
|
|
|
user := dbgen.User(t, db, database.User{})
|
|
|
|
_, token := dbgen.APIKey(t, db, database.APIKey{
|
|
|
|
UserID: user.ID,
|
2022-02-01 22:10:11 +00:00
|
|
|
})
|
2023-02-08 19:47:05 +00:00
|
|
|
organization := dbgen.Organization(t, db, database.Organization{})
|
|
|
|
_ = dbgen.OrganizationMember(t, db, database.OrganizationMember{
|
2022-02-01 22:10:11 +00:00
|
|
|
UserID: user.ID,
|
2023-02-08 19:47:05 +00:00
|
|
|
OrganizationID: organization.ID,
|
2022-02-01 22:10:11 +00:00
|
|
|
})
|
2023-02-08 19:47:05 +00:00
|
|
|
template := dbgen.Template(t, db, database.Template{
|
2022-02-01 22:10:11 +00:00
|
|
|
OrganizationID: organization.ID,
|
2023-01-23 11:14:47 +00:00
|
|
|
Provisioner: database.ProvisionerTypeEcho,
|
2022-02-01 22:10:11 +00:00
|
|
|
})
|
2023-02-08 19:47:05 +00:00
|
|
|
|
|
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
|
|
r.Header.Set(codersdk.SessionTokenHeader, token)
|
2022-02-01 22:10:11 +00:00
|
|
|
|
|
|
|
ctx := chi.NewRouteContext()
|
|
|
|
ctx.URLParams.Add("organization", organization.Name)
|
2022-04-06 17:42:40 +00:00
|
|
|
ctx.URLParams.Add("template", template.Name)
|
2022-02-01 22:10:11 +00:00
|
|
|
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, ctx))
|
2022-04-06 17:42:40 +00:00
|
|
|
return r, template
|
2022-02-01 22:10:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("None", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-10-30 17:42:20 +00:00
|
|
|
db := dbmem.New()
|
2022-02-01 22:10:11 +00:00
|
|
|
rtr := chi.NewRouter()
|
2022-04-06 17:42:40 +00:00
|
|
|
rtr.Use(httpmw.ExtractTemplateVersionParam(db))
|
2022-02-01 22:10:11 +00:00
|
|
|
rtr.Get("/", nil)
|
|
|
|
r, _ := setupAuthentication(db)
|
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
rtr.ServeHTTP(rw, r)
|
|
|
|
|
|
|
|
res := rw.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
require.Equal(t, http.StatusBadRequest, res.StatusCode)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("NotFound", func(t *testing.T) {
|
|
|
|
t.Parallel()
|
2023-10-30 17:42:20 +00:00
|
|
|
db := dbmem.New()
|
2022-02-01 22:10:11 +00:00
|
|
|
rtr := chi.NewRouter()
|
2022-04-06 17:42:40 +00:00
|
|
|
rtr.Use(httpmw.ExtractTemplateVersionParam(db))
|
2022-02-01 22:10:11 +00:00
|
|
|
rtr.Get("/", nil)
|
|
|
|
|
|
|
|
r, _ := setupAuthentication(db)
|
2022-04-06 17:42:40 +00:00
|
|
|
chi.RouteContext(r.Context()).URLParams.Add("templateversion", uuid.NewString())
|
2022-02-01 22:10:11 +00:00
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
rtr.ServeHTTP(rw, r)
|
|
|
|
|
|
|
|
res := rw.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
require.Equal(t, http.StatusNotFound, res.StatusCode)
|
|
|
|
})
|
|
|
|
|
2022-04-06 17:42:40 +00:00
|
|
|
t.Run("TemplateVersion", func(t *testing.T) {
|
2022-02-01 22:10:11 +00:00
|
|
|
t.Parallel()
|
2023-10-30 17:42:20 +00:00
|
|
|
db := dbmem.New()
|
2022-02-01 22:10:11 +00:00
|
|
|
rtr := chi.NewRouter()
|
|
|
|
rtr.Use(
|
2023-03-07 19:38:11 +00:00
|
|
|
httpmw.ExtractAPIKeyMW(httpmw.ExtractAPIKeyConfig{
|
2022-09-22 22:30:32 +00:00
|
|
|
DB: db,
|
|
|
|
RedirectToLogin: false,
|
|
|
|
}),
|
2022-04-06 17:42:40 +00:00
|
|
|
httpmw.ExtractTemplateVersionParam(db),
|
2022-03-07 17:40:54 +00:00
|
|
|
httpmw.ExtractOrganizationParam(db),
|
2022-02-01 22:10:11 +00:00
|
|
|
)
|
|
|
|
rtr.Get("/", func(rw http.ResponseWriter, r *http.Request) {
|
2022-04-06 17:42:40 +00:00
|
|
|
_ = httpmw.TemplateVersionParam(r)
|
2022-02-01 22:10:11 +00:00
|
|
|
rw.WriteHeader(http.StatusOK)
|
|
|
|
})
|
|
|
|
|
2022-04-06 17:42:40 +00:00
|
|
|
r, template := setupAuthentication(db)
|
2023-02-08 19:47:05 +00:00
|
|
|
templateVersion := dbgen.TemplateVersion(t, db, database.TemplateVersion{
|
2022-04-06 17:42:40 +00:00
|
|
|
OrganizationID: template.OrganizationID,
|
2022-02-01 22:10:11 +00:00
|
|
|
})
|
2022-04-06 17:42:40 +00:00
|
|
|
chi.RouteContext(r.Context()).URLParams.Add("templateversion", templateVersion.ID.String())
|
2022-02-01 22:10:11 +00:00
|
|
|
rw := httptest.NewRecorder()
|
|
|
|
rtr.ServeHTTP(rw, r)
|
|
|
|
|
|
|
|
res := rw.Result()
|
|
|
|
defer res.Body.Close()
|
|
|
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
|
|
|
})
|
|
|
|
}
|