|
| 1 | +package httpmw_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/go-chi/chi/v5" |
| 9 | + |
| 10 | + "github.com/coder/coder/coderd/httpmw" |
| 11 | + |
| 12 | + "github.com/coder/coder/coderd/database/dbauthz" |
| 13 | + "github.com/coder/coder/coderd/rbac" |
| 14 | + "github.com/google/uuid" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | +) |
| 17 | + |
| 18 | +func TestAsAuthzSystem(t *testing.T) { |
| 19 | + userActor := rbac.Subject{ID: uuid.NewString()} |
| 20 | + |
| 21 | + base := http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 22 | + actor, ok := dbauthz.ActorFromContext(r.Context()) |
| 23 | + assert.True(t, ok, "actor should exist") |
| 24 | + assert.True(t, userActor.Equal(actor), "actor should be the user actor") |
| 25 | + }) |
| 26 | + |
| 27 | + mwSetUser := func(next http.Handler) http.Handler { |
| 28 | + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 29 | + r = r.WithContext(dbauthz.As(r.Context(), userActor)) |
| 30 | + next.ServeHTTP(rw, r) |
| 31 | + }) |
| 32 | + } |
| 33 | + |
| 34 | + mwAssertSystem := mwAssert(func(req *http.Request) { |
| 35 | + actor, ok := dbauthz.ActorFromContext(req.Context()) |
| 36 | + assert.True(t, ok, "actor should exist") |
| 37 | + assert.False(t, userActor.Equal(actor), "systemActor should not be the user actor") |
| 38 | + assert.Contains(t, actor.Roles.Names(), "system", "should have system role") |
| 39 | + }) |
| 40 | + |
| 41 | + mwAssertUser := mwAssert(func(req *http.Request) { |
| 42 | + actor, ok := dbauthz.ActorFromContext(req.Context()) |
| 43 | + assert.True(t, ok, "actor should exist") |
| 44 | + assert.True(t, userActor.Equal(actor), "should be the useractor") |
| 45 | + }) |
| 46 | + |
| 47 | + mwAssertNoUser := mwAssert(func(req *http.Request) { |
| 48 | + _, ok := dbauthz.ActorFromContext(req.Context()) |
| 49 | + assert.False(t, ok, "actor should not exist") |
| 50 | + }) |
| 51 | + |
| 52 | + // Request as the user actor |
| 53 | + const pattern = "/" |
| 54 | + req := httptest.NewRequest("GET", pattern, nil) |
| 55 | + res := httptest.NewRecorder() |
| 56 | + |
| 57 | + handler := chi.NewRouter() |
| 58 | + handler.Route(pattern, func(r chi.Router) { |
| 59 | + r.Use( |
| 60 | + // First assert there is no actor context |
| 61 | + mwAssertNoUser, |
| 62 | + // Set to the user actor |
| 63 | + mwSetUser, |
| 64 | + // Assert the user actor |
| 65 | + mwAssertUser, |
| 66 | + httpmw.AsAuthzSystem( |
| 67 | + // Assert the system actor |
| 68 | + mwAssertSystem, |
| 69 | + mwAssertSystem, |
| 70 | + ), |
| 71 | + // Check the user actor was returned to the context |
| 72 | + mwAssertUser, |
| 73 | + ) |
| 74 | + r.Handle("/", base) |
| 75 | + r.NotFound(func(writer http.ResponseWriter, request *http.Request) { |
| 76 | + assert.Fail(t, "should not hit not found, the route should be correct") |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + handler.ServeHTTP(res, req) |
| 81 | +} |
| 82 | + |
| 83 | +func mwAssert(assert func(req *http.Request)) func(next http.Handler) http.Handler { |
| 84 | + return func(next http.Handler) http.Handler { |
| 85 | + return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { |
| 86 | + assert(r) |
| 87 | + next.ServeHTTP(rw, r) |
| 88 | + }) |
| 89 | + } |
| 90 | +} |
0 commit comments