-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add some more tests & refactor some code
Signed-off-by: Romain Caire <[email protected]>
- Loading branch information
1 parent
12d37b8
commit 9a55445
Showing
3 changed files
with
75 additions
and
45 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ package server | |
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
|
@@ -33,12 +34,10 @@ func TestGetTokenFromRequestSuccess(t *testing.T) { | |
var expectedToken = "aaa" | ||
var expectedTokenType = "bbb" | ||
|
||
reqBody := strings.NewReader( | ||
req := httptest.NewRequest(http.MethodPost, "https://test.tech/introspect", strings.NewReader( | ||
fmt.Sprintf(`token=%s&token_type_hint=%s`, expectedToken, expectedTokenType), | ||
) | ||
req := httptest.NewRequest(http.MethodPost, "https://test.tech/introspect", reqBody) | ||
)) | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
req.Header.Set("Content-Length", fmt.Sprint(reqBody.Len())) | ||
|
||
token, tokenType, err := s.getTokenFromRequest(req) | ||
if err != nil { | ||
|
@@ -81,6 +80,15 @@ func TestGetTokenFromRequestFailure(t *testing.T) { | |
desc: "The POST body can not be empty.", | ||
code: http.StatusBadRequest, | ||
}) | ||
|
||
req := httptest.NewRequest(http.MethodPost, "https://test.tech/introspect", strings.NewReader("token_type_hint=access_token")) | ||
req.Header.Add("Content-Type", "application/x-www-form-urlencoded") | ||
_, _, err = s.getTokenFromRequest(req) | ||
require.ErrorIs(t, err, &introspectionError{ | ||
typ: errInvalidRequest, | ||
desc: "The POST body doesn't contain 'token' parameter.", | ||
code: http.StatusBadRequest, | ||
}) | ||
} | ||
|
||
func TestIntrospectRefreshToken(t *testing.T) { | ||
|
@@ -107,30 +115,25 @@ func TestIntrospectRefreshToken(t *testing.T) { | |
require.NoError(t, err) | ||
require.EqualValues(t, &Introspection{ | ||
Active: true, | ||
Scope: "", | ||
ClientID: "test", | ||
Subject: "1", | ||
Subject: "CgExEgR0ZXN0", | ||
Expiry: t0.Add(s.refreshTokenPolicy.absoluteLifetime).Unix(), | ||
IssuedAt: t0.Unix(), | ||
NotBefore: t0.Unix(), | ||
Username: "", | ||
Audience: []string{ | ||
"test", | ||
}, | ||
Issuer: s.issuerURL.String(), | ||
JwtTokenID: "test", | ||
TokenType: "Bearer", | ||
TokenUse: "refresh_token", | ||
Issuer: s.issuerURL.String(), | ||
TokenType: "Bearer", | ||
TokenUse: "refresh_token", | ||
Extra: IntrospectionExtra{ | ||
AuthorizingParty: "", | ||
Email: "[email protected]", | ||
EmailVerified: &trueValue, | ||
Email: "[email protected]", | ||
EmailVerified: &trueValue, | ||
Groups: []string{ | ||
"a", | ||
"b", | ||
}, | ||
Name: "jane", | ||
PreferredUsername: "", | ||
Name: "jane", | ||
}, | ||
}, introspection) | ||
} | ||
|
@@ -157,38 +160,56 @@ func TestIntrospectAccessToken(t *testing.T) { | |
Email: "[email protected]", | ||
EmailVerified: true, | ||
Groups: []string{"a", "b"}, | ||
}, []string{"openid", "email", "profile", "groups"}, "foo", "bar", "code", "test") | ||
}, []string{"openid", "email", "profile", "groups"}, "foo", "", "", "test") | ||
require.NoError(t, err) | ||
|
||
// Nominal test-case | ||
// Nominal active test-case | ||
introspection, err := s.introspectAccessToken(context.Background(), token) | ||
require.NoError(t, err) | ||
require.ErrorIs(t, err, nil) | ||
require.EqualValues(t, &Introspection{ | ||
Active: true, | ||
Scope: "", | ||
ClientID: "test", | ||
Subject: "CgExEgR0ZXN0", | ||
Expiry: expiry.Unix(), | ||
IssuedAt: t0.Unix(), | ||
NotBefore: t0.Unix(), | ||
Username: "", | ||
Audience: []string{ | ||
"test", | ||
}, | ||
Issuer: s.issuerURL.String(), | ||
JwtTokenID: "_N4rLtula_QIYB-3If6bXA", | ||
TokenType: "Bearer", | ||
TokenUse: "access_token", | ||
Issuer: s.issuerURL.String(), | ||
TokenType: "Bearer", | ||
TokenUse: "access_token", | ||
Extra: IntrospectionExtra{ | ||
AuthorizingParty: "", | ||
Email: "[email protected]", | ||
EmailVerified: &trueValue, | ||
Email: "[email protected]", | ||
EmailVerified: &trueValue, | ||
Groups: []string{ | ||
"a", | ||
"b", | ||
}, | ||
Name: "jane", | ||
PreferredUsername: "", | ||
Name: "jane", | ||
}, | ||
}, introspection) | ||
|
||
// Nominal inactive test-case | ||
introspection, err = s.introspectAccessToken(context.Background(), "token") | ||
require.ErrorIs(t, err, &introspectionError{ | ||
typ: errInactiveToken, | ||
code: http.StatusUnauthorized, | ||
}) | ||
require.EqualValues(t, introspection, (*Introspection)(nil)) | ||
} | ||
|
||
func TestIntrospectInactiveErr(t *testing.T) { | ||
writeRecorder := httptest.NewRecorder() | ||
err := introspectInactiveErr(writeRecorder) | ||
require.NoError(t, err) | ||
|
||
res := writeRecorder.Result() | ||
require.Equal(t, http.StatusUnauthorized, res.StatusCode) | ||
require.Equal(t, "application/json", res.Header.Get("Content-Type")) | ||
|
||
data, err := io.ReadAll(res.Body) | ||
defer res.Body.Close() | ||
require.NoError(t, err) | ||
require.Equal(t, `{"active":false}`, string(data)) | ||
} |
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