- Notifications
You must be signed in to change notification settings - Fork928
chore: improve testing coverage on ExtractProvisionerDaemonAuthenticated middleware#15622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes from1 commit
Commits
Show all changes
8 commits Select commitHold shift + click to select a range
1bfbf46
work-on-testing-cases
defelmnqd9ab576
improve testing coverage
defelmnqe26f5e1
improve testing coverage
defelmnq13d6dd9
improve testing coverage
defelmnqa6afc33
move middlewar
defelmnq66bd65e
move middlewar
defelmnq5eafd6d
improve testing coverage
defelmnq711eeee
improve testing coverage
defelmnqFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
NextNext commit
work-on-testing-cases
- Loading branch information
Uh oh!
There was an error while loading.Please reload this page.
commit1bfbf46e84791ad465db651082db194d0fd082d2
There are no files selected for viewing
148 changes: 148 additions & 0 deletionscoderd/httpmw/provisionerdaemon_test.go
This file contains hidden or 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,148 @@ | ||
package httpmw_test | ||
import ( | ||
"context" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
"github.com/go-chi/chi/v5" | ||
"github.com/stretchr/testify/require" | ||
"github.com/coder/coder/v2/coderd/httpmw" | ||
"github.com/coder/coder/v2/codersdk" | ||
) | ||
func TestExtractProvisionerDaemonAuthenticated(t *testing.T) { | ||
t.Parallel() | ||
tests := []struct { | ||
name string | ||
opts httpmw.ExtractProvisionerAuthConfig | ||
expectedStatusCode int | ||
expectedResponseMessage string | ||
provisionerKey string | ||
provisionerPSK string | ||
}{ | ||
{ | ||
name: "NoKeyProvided_Optional", | ||
opts: httpmw.ExtractProvisionerAuthConfig{ | ||
DB: nil, | ||
Optional: true, | ||
}, | ||
expectedStatusCode: http.StatusOK, | ||
expectedResponseMessage: "", | ||
}, | ||
{ | ||
name: "NoKeyProvided_NotOptional", | ||
opts: httpmw.ExtractProvisionerAuthConfig{ | ||
DB: nil, | ||
Optional: false, | ||
}, | ||
expectedStatusCode: http.StatusUnauthorized, | ||
expectedResponseMessage: "provisioner daemon key required", | ||
}, | ||
{ | ||
name: "ProvisionerKeyAndPSKProvided_NotOptional", | ||
opts: httpmw.ExtractProvisionerAuthConfig{ | ||
DB: nil, | ||
Optional: false, | ||
}, | ||
provisionerKey: "key", | ||
provisionerPSK: "psk", | ||
expectedStatusCode: http.StatusBadRequest, | ||
expectedResponseMessage: "provisioner daemon key and psk provided, but only one is allowed", | ||
}, | ||
{ | ||
name: "ProvisionerKeyAndPSKProvided_Optional", | ||
opts: httpmw.ExtractProvisionerAuthConfig{ | ||
DB: nil, | ||
Optional: true, | ||
}, | ||
provisionerKey: "key", | ||
expectedStatusCode: http.StatusOK, | ||
expectedResponseMessage: "", | ||
}, | ||
{ | ||
name: "InvalidProvisionerKey_NotOptional", | ||
opts: httpmw.ExtractProvisionerAuthConfig{ | ||
DB: nil, | ||
Optional: false, | ||
}, | ||
provisionerKey: "invalid", | ||
expectedStatusCode: http.StatusBadRequest, | ||
expectedResponseMessage: "provisioner daemon key invalid", | ||
}, | ||
{ | ||
name: "InvalidProvisionerKey_Optional", | ||
opts: httpmw.ExtractProvisionerAuthConfig{ | ||
DB: nil, | ||
Optional: true, | ||
}, | ||
provisionerKey: "invalid", | ||
expectedStatusCode: http.StatusOK, | ||
expectedResponseMessage: "", | ||
}, | ||
{ | ||
name: "InvalidProvisionerPSK_NotOptional", | ||
opts: httpmw.ExtractProvisionerAuthConfig{ | ||
DB: nil, | ||
Optional: false, | ||
PSK: "psk", | ||
}, | ||
provisionerPSK: "invalid", | ||
expectedStatusCode: http.StatusUnauthorized, | ||
expectedResponseMessage: "provisioner daemon psk invalid", | ||
}, | ||
{ | ||
name: "InvalidProvisionerPSK_Optional", | ||
opts: httpmw.ExtractProvisionerAuthConfig{ | ||
DB: nil, | ||
Optional: true, | ||
PSK: "psk", | ||
}, | ||
provisionerPSK: "invalid", | ||
expectedStatusCode: http.StatusOK, | ||
expectedResponseMessage: "", | ||
}, | ||
{ | ||
name: "ValidProvisionerPSK_NotOptional", | ||
opts: httpmw.ExtractProvisionerAuthConfig{ | ||
DB: nil, | ||
Optional: false, | ||
PSK: "ThisIsAValidPSK", | ||
}, | ||
provisionerPSK: "ThisIsAValidPSK", | ||
expectedStatusCode: http.StatusOK, | ||
expectedResponseMessage: "", | ||
}, | ||
} | ||
for _, test := range tests { | ||
test := test | ||
t.Run(test.name, func(t *testing.T) { | ||
t.Parallel() | ||
routeCtx := chi.NewRouteContext() | ||
r := httptest.NewRequest(http.MethodGet, "/", nil) | ||
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, routeCtx)) | ||
res := httptest.NewRecorder() | ||
if test.provisionerKey != "" { | ||
r.Header.Set(codersdk.ProvisionerDaemonKey, test.provisionerKey) | ||
} | ||
if test.provisionerPSK != "" { | ||
r.Header.Set(codersdk.ProvisionerDaemonPSK, test.provisionerPSK) | ||
} | ||
httpmw.ExtractProvisionerDaemonAuthenticated(test.opts)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
})).ServeHTTP(res, r) | ||
require.Equal(t, test.expectedStatusCode, res.Result().StatusCode) | ||
if test.expectedResponseMessage != "" { | ||
require.Contains(t, res.Body.String(), test.expectedResponseMessage) | ||
} | ||
}) | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.