mirror of
https://github.com/coder/coder.git
synced 2025-07-03 16:13:58 +00:00
* chore: Use dbgen in unit test - organizationparam_test - templateparam_test * Use dbgen in all unit tests vs insert methods * fixup! Use dbgen in all unit tests vs insert methods --------- Co-authored-by: Cian Johnston <cian@coder.com>
69 lines
1.5 KiB
Go
69 lines
1.5 KiB
Go
package httpmw_test
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
"github.com/google/uuid"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/coderd/database"
|
|
"github.com/coder/coder/coderd/database/dbfake"
|
|
"github.com/coder/coder/coderd/database/dbgen"
|
|
"github.com/coder/coder/coderd/httpmw"
|
|
"github.com/coder/coder/codersdk"
|
|
)
|
|
|
|
func TestWorkspaceAgent(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
setup := func(db database.Store) (*http.Request, uuid.UUID) {
|
|
token := uuid.New()
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
r.Header.Set(codersdk.SessionTokenHeader, token.String())
|
|
return r, token
|
|
}
|
|
|
|
t.Run("None", func(t *testing.T) {
|
|
t.Parallel()
|
|
db := dbfake.New()
|
|
rtr := chi.NewRouter()
|
|
rtr.Use(
|
|
httpmw.ExtractWorkspaceAgent(db),
|
|
)
|
|
rtr.Get("/", nil)
|
|
r, _ := setup(db)
|
|
rw := httptest.NewRecorder()
|
|
rtr.ServeHTTP(rw, r)
|
|
|
|
res := rw.Result()
|
|
defer res.Body.Close()
|
|
require.Equal(t, http.StatusUnauthorized, res.StatusCode)
|
|
})
|
|
|
|
t.Run("Found", func(t *testing.T) {
|
|
t.Parallel()
|
|
db := dbfake.New()
|
|
rtr := chi.NewRouter()
|
|
rtr.Use(
|
|
httpmw.ExtractWorkspaceAgent(db),
|
|
)
|
|
rtr.Get("/", func(rw http.ResponseWriter, r *http.Request) {
|
|
_ = httpmw.WorkspaceAgent(r)
|
|
rw.WriteHeader(http.StatusOK)
|
|
})
|
|
r, token := setup(db)
|
|
_ = dbgen.WorkspaceAgent(t, db, database.WorkspaceAgent{
|
|
AuthToken: token,
|
|
})
|
|
rw := httptest.NewRecorder()
|
|
rtr.ServeHTTP(rw, r)
|
|
|
|
res := rw.Result()
|
|
defer res.Body.Close()
|
|
require.Equal(t, http.StatusOK, res.StatusCode)
|
|
})
|
|
}
|