-
-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: auto redirect to summary page for any authentication mechanism (r…
…esolve #589)
- Loading branch information
Showing
5 changed files
with
185 additions
and
5 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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package mocks | ||
|
||
import ( | ||
"github.com/muety/wakapi/models" | ||
"github.com/stretchr/testify/mock" | ||
) | ||
|
||
type KeyValueServiceMock struct { | ||
mock.Mock | ||
} | ||
|
||
func (m *KeyValueServiceMock) GetString(s string) (*models.KeyStringValue, error) { | ||
args := m.Called(s) | ||
return args.Get(0).(*models.KeyStringValue), args.Error(1) | ||
} | ||
|
||
func (m *KeyValueServiceMock) MustGetString(s string) *models.KeyStringValue { | ||
args := m.Called(s) | ||
return args.Get(0).(*models.KeyStringValue) | ||
} | ||
|
||
func (m *KeyValueServiceMock) GetByPrefix(s string) ([]*models.KeyStringValue, error) { | ||
args := m.Called(s) | ||
return args.Get(0).([]*models.KeyStringValue), args.Error(1) | ||
} | ||
|
||
func (m *KeyValueServiceMock) PutString(v *models.KeyStringValue) error { | ||
args := m.Called(v) | ||
return args.Error(0) | ||
} | ||
|
||
func (m *KeyValueServiceMock) DeleteString(s string) error { | ||
args := m.Called(s) | ||
return args.Error(0) | ||
} |
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,123 @@ | ||
package routes | ||
|
||
import ( | ||
"github.com/go-chi/chi/v5" | ||
"github.com/muety/wakapi/config" | ||
"github.com/muety/wakapi/middlewares" | ||
"github.com/muety/wakapi/mocks" | ||
"github.com/muety/wakapi/models" | ||
"github.com/stretchr/testify/assert" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"strings" | ||
"testing" | ||
) | ||
|
||
var ( | ||
user1 = models.User{ | ||
ID: "user1", | ||
ShareDataMaxDays: 30, | ||
ShareLanguages: true, | ||
ApiKey: "fakekey", | ||
} | ||
) | ||
|
||
func TestHomeHandler_Get_NotLoggedIn(t *testing.T) { | ||
config.Set(config.Empty()) | ||
config.Get().Env = "dev" | ||
|
||
if cwd, _ := os.Getwd(); strings.HasSuffix(cwd, "routes") { | ||
os.Chdir("..") | ||
} | ||
|
||
router := chi.NewRouter() | ||
router.Use(middlewares.NewPrincipalMiddleware()) | ||
|
||
userServiceMock := new(mocks.UserServiceMock) | ||
userServiceMock.On("GetUserById", user1.ID).Return(&user1, nil) | ||
|
||
keyValueServiceMock := new(mocks.KeyValueServiceMock) | ||
keyValueServiceMock.On("GetString", config.KeyLatestTotalTime).Return(&models.KeyStringValue{Key: config.KeyLatestTotalTime, Value: "0"}, nil) | ||
keyValueServiceMock.On("GetString", config.KeyLatestTotalUsers).Return(&models.KeyStringValue{Key: config.KeyLatestTotalUsers, Value: "0"}, nil) | ||
keyValueServiceMock.On("GetString", config.KeyNewsbox).Return(&models.KeyStringValue{Key: config.KeyNewsbox, Value: ""}, nil) | ||
|
||
homeHandler := NewHomeHandler(userServiceMock, keyValueServiceMock) | ||
homeHandler.RegisterRoutes(router) | ||
|
||
t.Run("when requesting frontpage", func(t *testing.T) { | ||
t.Run("should display it without authentication", func(t *testing.T) { | ||
rec := httptest.NewRecorder() | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
|
||
router.ServeHTTP(rec, req) | ||
res := rec.Result() | ||
defer res.Body.Close() | ||
|
||
assert.Equal(t, http.StatusOK, res.StatusCode) | ||
|
||
data, err := ioutil.ReadAll(res.Body) | ||
if err != nil { | ||
t.Errorf("unextected error. Error: %s", err) | ||
} | ||
|
||
assert.Contains(t, string(data), "<a href=\"login\" class=\"btn-primary\">") | ||
keyValueServiceMock.AssertNumberOfCalls(t, "GetString", 3) | ||
}) | ||
}) | ||
} | ||
|
||
func TestHomeHandler_Get_LoggedIn(t *testing.T) { | ||
config.Set(config.Empty()) | ||
|
||
router := chi.NewRouter() | ||
router.Use(middlewares.NewPrincipalMiddleware()) | ||
|
||
userServiceMock := new(mocks.UserServiceMock) | ||
userServiceMock.On("GetUserByKey", user1.ApiKey).Return(&user1, nil) | ||
userServiceMock.On("GetUserById", user1.ID).Return(&user1, nil) | ||
|
||
keyValueServiceMock := new(mocks.KeyValueServiceMock) | ||
|
||
homeHandler := NewHomeHandler(userServiceMock, keyValueServiceMock) | ||
homeHandler.RegisterRoutes(router) | ||
|
||
t.Run("when requesting frontpage", func(t *testing.T) { | ||
t.Run("should redirect in case of api key auth", func(t *testing.T) { | ||
rec := httptest.NewRecorder() | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
q := req.URL.Query() | ||
q.Set("api_key", user1.ApiKey) | ||
req.URL.RawQuery = q.Encode() | ||
|
||
router.ServeHTTP(rec, req) | ||
res := rec.Result() | ||
defer res.Body.Close() | ||
|
||
assert.Equal(t, http.StatusFound, res.StatusCode) | ||
}) | ||
|
||
t.Run("should redirect in case of trusted header auth", func(t *testing.T) { | ||
c := config.Get() | ||
c.Security.TrustedHeaderAuth = true | ||
c.Security.TrustedHeaderAuthKey = "Remote-User" | ||
c.Security.TrustReverseProxyIps = "127.0.0.1" | ||
c.Security.ParseTrustReverseProxyIPs() | ||
|
||
rec := httptest.NewRecorder() | ||
|
||
req := httptest.NewRequest(http.MethodGet, "/", nil) | ||
req.Header.Set("Remote-User", user1.ID) | ||
req.RemoteAddr = "127.0.0.1:12345" | ||
|
||
router.ServeHTTP(rec, req) | ||
res := rec.Result() | ||
defer res.Body.Close() | ||
|
||
assert.Equal(t, http.StatusFound, res.StatusCode) | ||
}) | ||
}) | ||
} |
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,17 @@ | ||
package routes | ||
|
||
import ( | ||
"context" | ||
"github.com/go-chi/chi/v5" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func withUrlParam(r *http.Request, key, value string) *http.Request { | ||
r.URL.RawPath = strings.Replace(r.URL.RawPath, "{"+key+"}", value, 1) | ||
r.URL.Path = strings.Replace(r.URL.Path, "{"+key+"}", value, 1) | ||
rctx := chi.NewRouteContext() | ||
rctx.URLParams.Add(key, value) | ||
r = r.WithContext(context.WithValue(r.Context(), chi.RouteCtxKey, rctx)) | ||
return r | ||
} |