Skip to content

Commit

Permalink
Tests: Add unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
graciousgrey committed May 7, 2024
1 parent ca52f0e commit 38a1616
Showing 1 changed file with 42 additions and 1 deletion.
43 changes: 42 additions & 1 deletion internal/api/oauth_revoke_token_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func TestRevokeOAuthToken(t *testing.T) {
const tokenPath = "/api/v1/oauth/token"
const revokePath = "/api/v1/oauth/revoke"

t.Run("Success", func(t *testing.T) {
t.Run("ClientSuccess", func(t *testing.T) {
app, router, conf := NewApiTest()
conf.SetAuthMode(config.AuthModePasswd)
defer conf.SetAuthMode(config.AuthModePublic)
Expand Down Expand Up @@ -114,4 +114,45 @@ func TestRevokeOAuthToken(t *testing.T) {
t.Logf("BODY: %s", revokeResp.Body.String())
assert.Equal(t, http.StatusForbidden, revokeResp.Code)
})
t.Run("UserSuccess", func(t *testing.T) {
app, router, conf := NewApiTest()
conf.SetAuthMode(config.AuthModePasswd)
defer conf.SetAuthMode(config.AuthModePublic)

sessId := AuthenticateUser(app, router, "alice", "Alice123!")

CreateOAuthToken(router)
RevokeOAuthToken(router)

data := url.Values{
"grant_type": {"password"},
"client_name": {"AppPasswordAlice"},
"username": {"alice"},
"password": {"Alice123!"},
"scope": {"metrics"},
}

createToken, _ := http.NewRequest("POST", tokenPath, strings.NewReader(data.Encode()))
createToken.Header.Add(header.ContentType, header.ContentTypeForm)
createToken.Header.Add(header.XAuthToken, sessId)

createResp := httptest.NewRecorder()
app.ServeHTTP(createResp, createToken)

t.Logf("Header: %s", createResp.Header())
t.Logf("BODY: %s", createResp.Body.String())
assert.Equal(t, http.StatusOK, createResp.Code)
authToken := gjson.Get(createResp.Body.String(), "access_token").String()

revokeToken, _ := http.NewRequest("POST", revokePath, nil)
revokeToken.Header.Add(header.XAuthToken, authToken)
createToken.Header.Add(header.XAuthToken, sessId)

revokeResp := httptest.NewRecorder()
app.ServeHTTP(revokeResp, revokeToken)

t.Logf("Header: %s", revokeResp.Header())
t.Logf("BODY: %s", revokeResp.Body.String())
assert.Equal(t, http.StatusOK, revokeResp.Code)
})
}

0 comments on commit 38a1616

Please sign in to comment.