mirror of https://github.com/artifacthub/hub.git
Add some more tests for users handlers (#1194)
Signed-off-by: Sergio Castaño Arteaga <tegioz@icloud.com>
This commit is contained in:
parent
22f00799a1
commit
d30b49cde8
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"math/big"
|
"math/big"
|
||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -122,7 +123,7 @@ func (h *Handlers) BasicAuth(next http.Handler) http.Handler {
|
||||||
if !ok || !areCredentialsValid([]byte(user), []byte(pass)) {
|
if !ok || !areCredentialsValid([]byte(user), []byte(pass)) {
|
||||||
w.Header().Set("WWW-Authenticate", "Basic realm="+realm+`"`)
|
w.Header().Set("WWW-Authenticate", "Basic realm="+realm+`"`)
|
||||||
w.WriteHeader(http.StatusUnauthorized)
|
w.WriteHeader(http.StatusUnauthorized)
|
||||||
_, _ = w.Write([]byte("Unauthorized\n"))
|
_, _ = io.WriteString(w, "Unauthorized\n")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
next.ServeHTTP(w, r)
|
next.ServeHTTP(w, r)
|
||||||
|
|
@ -461,6 +462,8 @@ func (h *Handlers) registerUserWithOauth(
|
||||||
u, err = h.newUserFromGoogleProfile(ctx, providerConfig, oauthToken)
|
u, err = h.newUserFromGoogleProfile(ctx, providerConfig, oauthToken)
|
||||||
case "oidc":
|
case "oidc":
|
||||||
u, err = h.newUserFromOIDProfile(ctx, oauthToken)
|
u, err = h.newUserFromOIDProfile(ctx, oauthToken)
|
||||||
|
default:
|
||||||
|
err = fmt.Errorf("invalid provider: %s", provider)
|
||||||
}
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
|
|
|
||||||
|
|
@ -495,6 +495,100 @@ func TestLogout(t *testing.T) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestOauthCallback(t *testing.T) {
|
||||||
|
t.Run("invalid oauth code or state", func(t *testing.T) {
|
||||||
|
state := &OauthState{
|
||||||
|
Random: "abcd",
|
||||||
|
RedirectURL: "/",
|
||||||
|
}
|
||||||
|
|
||||||
|
testCases := []struct {
|
||||||
|
description string
|
||||||
|
url string
|
||||||
|
cookie *http.Cookie
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
"oauth code not provided",
|
||||||
|
"/",
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"oauth state not provided",
|
||||||
|
"/?code=1234",
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"state cookie not provided",
|
||||||
|
"/?code=1234&state=" + state.String(),
|
||||||
|
nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"invalid state cookie",
|
||||||
|
"/?code=1234&state=" + state.String(),
|
||||||
|
&http.Cookie{
|
||||||
|
Name: oauthStateCookieName,
|
||||||
|
Value: "something not expected",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range testCases {
|
||||||
|
tc := tc
|
||||||
|
t.Run(tc.description, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
r, _ := http.NewRequest("GET", tc.url, nil)
|
||||||
|
if tc.cookie != nil {
|
||||||
|
r.AddCookie(tc.cookie)
|
||||||
|
}
|
||||||
|
|
||||||
|
hw := newHandlersWrapper()
|
||||||
|
hw.h.OauthCallback(w, r)
|
||||||
|
resp := w.Result()
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
assert.Equal(t, http.StatusSeeOther, resp.StatusCode)
|
||||||
|
redirectURL, err := resp.Location()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, oauthFailedURL, redirectURL.String())
|
||||||
|
})
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestOauthRedirect(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
w := httptest.NewRecorder()
|
||||||
|
r, _ := http.NewRequest("GET", "/", nil)
|
||||||
|
rctx := &chi.Context{
|
||||||
|
URLParams: chi.RouteParams{
|
||||||
|
Keys: []string{"provider"},
|
||||||
|
Values: []string{"github"},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx))
|
||||||
|
|
||||||
|
hw := newHandlersWrapper()
|
||||||
|
hw.h.OauthRedirect(w, r)
|
||||||
|
resp := w.Result()
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
require.Len(t, resp.Cookies(), 1)
|
||||||
|
assert.Equal(t, oauthStateCookieName, resp.Cookies()[0].Name)
|
||||||
|
assert.NotEmpty(t, resp.Cookies()[0].Value)
|
||||||
|
assert.Equal(t, "/", resp.Cookies()[0].Path)
|
||||||
|
assert.True(t, resp.Cookies()[0].HttpOnly)
|
||||||
|
assert.False(t, resp.Cookies()[0].Secure)
|
||||||
|
assert.Equal(t, http.StatusSeeOther, resp.StatusCode)
|
||||||
|
state := &OauthState{
|
||||||
|
Random: resp.Cookies()[0].Value,
|
||||||
|
RedirectURL: "/",
|
||||||
|
}
|
||||||
|
expectedRedirectURL := hw.h.oauthConfig["github"].AuthCodeURL(state.String())
|
||||||
|
redirectURL, err := resp.Location()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, expectedRedirectURL, redirectURL.String())
|
||||||
|
}
|
||||||
|
|
||||||
func TestRegisterPasswordResetCode(t *testing.T) {
|
func TestRegisterPasswordResetCode(t *testing.T) {
|
||||||
t.Run("invalid input", func(t *testing.T) {
|
t.Run("invalid input", func(t *testing.T) {
|
||||||
t.Parallel()
|
t.Parallel()
|
||||||
|
|
@ -1173,6 +1267,7 @@ type handlersWrapper struct {
|
||||||
func newHandlersWrapper() *handlersWrapper {
|
func newHandlersWrapper() *handlersWrapper {
|
||||||
cfg := viper.New()
|
cfg := viper.New()
|
||||||
cfg.Set("server.baseURL", "baseURL")
|
cfg.Set("server.baseURL", "baseURL")
|
||||||
|
cfg.Set("server.oauth.github", map[string]string{})
|
||||||
um := &user.ManagerMock{}
|
um := &user.ManagerMock{}
|
||||||
h, _ := NewHandlers(context.Background(), um, cfg)
|
h, _ := NewHandlers(context.Background(), um, cfg)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
@ -25,7 +26,7 @@ func TestSetupConfig(t *testing.T) {
|
||||||
f, err := os.Create(name)
|
f, err := os.Create(name)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
defer os.Remove(name)
|
defer os.Remove(name)
|
||||||
_, err = f.Write([]byte(`key1: value1`))
|
_, err = io.WriteString(f, `key1: value1`)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// Check SetupConfig now succeeds
|
// Check SetupConfig now succeeds
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue