-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add history middleware parameters (#129)
These will be used for streaming logs, checking status, and other operations related to workspace and project history.
- Loading branch information
Showing
9 changed files
with
455 additions
and
4 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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,60 @@ | ||
package httpmw | ||
|
||
import ( | ||
"context" | ||
"database/sql" | ||
"errors" | ||
"fmt" | ||
"net/http" | ||
|
||
"github.com/go-chi/chi/v5" | ||
|
||
"github.com/coder/coder/database" | ||
"github.com/coder/coder/httpapi" | ||
) | ||
|
||
type projectHistoryParamContextKey struct{} | ||
|
||
// ProjectHistoryParam returns the project history from the ExtractProjectHistoryParam handler. | ||
func ProjectHistoryParam(r *http.Request) database.ProjectHistory { | ||
projectHistory, ok := r.Context().Value(projectHistoryParamContextKey{}).(database.ProjectHistory) | ||
if !ok { | ||
panic("developer error: project history param middleware not provided") | ||
} | ||
return projectHistory | ||
} | ||
|
||
// ExtractProjectHistoryParam grabs project history from the "projecthistory" URL parameter. | ||
func ExtractProjectHistoryParam(db database.Store) func(http.Handler) http.Handler { | ||
return func(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) { | ||
project := ProjectParam(r) | ||
projectHistoryName := chi.URLParam(r, "projecthistory") | ||
if projectHistoryName == "" { | ||
httpapi.Write(rw, http.StatusBadRequest, httpapi.Response{ | ||
Message: "project history name must be provided", | ||
}) | ||
return | ||
} | ||
projectHistory, err := db.GetProjectHistoryByProjectIDAndName(r.Context(), database.GetProjectHistoryByProjectIDAndNameParams{ | ||
ProjectID: project.ID, | ||
Name: projectHistoryName, | ||
}) | ||
if errors.Is(err, sql.ErrNoRows) { | ||
httpapi.Write(rw, http.StatusNotFound, httpapi.Response{ | ||
Message: fmt.Sprintf("project history %q does not exist", projectHistoryName), | ||
}) | ||
return | ||
} | ||
if err != nil { | ||
httpapi.Write(rw, http.StatusInternalServerError, httpapi.Response{ | ||
Message: fmt.Sprintf("get project history: %s", err.Error()), | ||
}) | ||
return | ||
} | ||
|
||
ctx := context.WithValue(r.Context(), projectHistoryParamContextKey{}, projectHistory) | ||
next.ServeHTTP(rw, r.WithContext(ctx)) | ||
}) | ||
} | ||
} |
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,161 @@ | ||
package httpmw_test | ||
|
||
import ( | ||
"context" | ||
"crypto/sha256" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"time" | ||
|
||
"github.com/go-chi/chi/v5" | ||
"github.com/google/uuid" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/coder/coder/cryptorand" | ||
"github.com/coder/coder/database" | ||
"github.com/coder/coder/database/databasefake" | ||
"github.com/coder/coder/httpmw" | ||
) | ||
|
||
func TestProjectHistoryParam(t *testing.T) { | ||
t.Parallel() | ||
|
||
setupAuthentication := func(db database.Store) (*http.Request, database.Project) { | ||
var ( | ||
id, secret = randomAPIKeyParts() | ||
hashed = sha256.Sum256([]byte(secret)) | ||
) | ||
r := httptest.NewRequest("GET", "/", nil) | ||
r.AddCookie(&http.Cookie{ | ||
Name: httpmw.AuthCookie, | ||
Value: fmt.Sprintf("%s-%s", id, secret), | ||
}) | ||
userID, err := cryptorand.String(16) | ||
require.NoError(t, err) | ||
username, err := cryptorand.String(8) | ||
require.NoError(t, err) | ||
user, err := db.InsertUser(r.Context(), database.InsertUserParams{ | ||
ID: userID, | ||
Email: "testaccount@coder.com", | ||
Name: "example", | ||
LoginType: database.LoginTypeBuiltIn, | ||
HashedPassword: hashed[:], | ||
Username: username, | ||
CreatedAt: database.Now(), | ||
UpdatedAt: database.Now(), | ||
}) | ||
require.NoError(t, err) | ||
_, err = db.InsertAPIKey(r.Context(), database.InsertAPIKeyParams{ | ||
ID: id, | ||
UserID: user.ID, | ||
HashedSecret: hashed[:], | ||
LastUsed: database.Now(), | ||
ExpiresAt: database.Now().Add(time.Minute), | ||
}) | ||
require.NoError(t, err) | ||
orgID, err := cryptorand.String(16) | ||
require.NoError(t, err) | ||
organization, err := db.InsertOrganization(r.Context(), database.InsertOrganizationParams{ | ||
ID: orgID, | ||
Name: "banana", | ||
Description: "wowie", | ||
CreatedAt: database.Now(), | ||
UpdatedAt: database.Now(), | ||
}) | ||
require.NoError(t, err) | ||
_, err = db.InsertOrganizationMember(r.Context(), database.InsertOrganizationMemberParams{ | ||
OrganizationID: orgID, | ||
UserID: user.ID, | ||
CreatedAt: database.Now(), | ||
UpdatedAt: database.Now(), | ||
}) | ||
require.NoError(t, err) | ||
project, err := db.InsertProject(context.Background(), database.InsertProjectParams{ | ||
ID: uuid.New(), | ||
OrganizationID: organization.ID, | ||
Name: "moo", | ||
}) | ||
require.NoError(t, err) | ||
|
||
ctx := chi.NewRouteContext() | ||
ctx.URLParams.Add("organization", organization.Name) | ||
ctx.URLParams.Add("project", project.Name) | ||
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, ctx)) | ||
return r, project | ||
} | ||
|
||
t.Run("None", func(t *testing.T) { | ||
t.Parallel() | ||
db := databasefake.New() | ||
rtr := chi.NewRouter() | ||
rtr.Use( | ||
httpmw.ExtractAPIKey(db, nil), | ||
httpmw.ExtractOrganizationParam(db), | ||
httpmw.ExtractProjectParam(db), | ||
httpmw.ExtractProjectHistoryParam(db), | ||
) | ||
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() | ||
db := databasefake.New() | ||
rtr := chi.NewRouter() | ||
rtr.Use( | ||
httpmw.ExtractAPIKey(db, nil), | ||
httpmw.ExtractOrganizationParam(db), | ||
httpmw.ExtractProjectParam(db), | ||
httpmw.ExtractProjectHistoryParam(db), | ||
) | ||
rtr.Get("/", nil) | ||
|
||
r, _ := setupAuthentication(db) | ||
chi.RouteContext(r.Context()).URLParams.Add("projecthistory", "nothin") | ||
rw := httptest.NewRecorder() | ||
rtr.ServeHTTP(rw, r) | ||
|
||
res := rw.Result() | ||
defer res.Body.Close() | ||
require.Equal(t, http.StatusNotFound, res.StatusCode) | ||
}) | ||
|
||
t.Run("ProjectHistory", func(t *testing.T) { | ||
t.Parallel() | ||
db := databasefake.New() | ||
rtr := chi.NewRouter() | ||
rtr.Use( | ||
httpmw.ExtractAPIKey(db, nil), | ||
httpmw.ExtractOrganizationParam(db), | ||
httpmw.ExtractProjectParam(db), | ||
httpmw.ExtractProjectHistoryParam(db), | ||
) | ||
rtr.Get("/", func(rw http.ResponseWriter, r *http.Request) { | ||
_ = httpmw.ProjectHistoryParam(r) | ||
rw.WriteHeader(http.StatusOK) | ||
}) | ||
|
||
r, project := setupAuthentication(db) | ||
projectHistory, err := db.InsertProjectHistory(context.Background(), database.InsertProjectHistoryParams{ | ||
ID: uuid.New(), | ||
ProjectID: project.ID, | ||
Name: "moo", | ||
}) | ||
require.NoError(t, err) | ||
chi.RouteContext(r.Context()).URLParams.Add("projecthistory", projectHistory.Name) | ||
rw := httptest.NewRecorder() | ||
rtr.ServeHTTP(rw, r) | ||
|
||
res := rw.Result() | ||
defer res.Body.Close() | ||
require.Equal(t, http.StatusOK, res.StatusCode) | ||
}) | ||
} |
Oops, something went wrong.