mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
refactor: migrate auth routes to v1 package (#1841)
* feat: add api v1 packages * chore: migrate auth to v1 * chore: update test
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/usememos/memos/api"
|
||||
apiv1 "github.com/usememos/memos/api/v1"
|
||||
)
|
||||
|
||||
func TestAuthServer(t *testing.T) {
|
||||
@@ -17,7 +18,7 @@ func TestAuthServer(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer s.Shutdown(ctx)
|
||||
|
||||
signup := &api.SignUp{
|
||||
signup := &apiv1.SignUp{
|
||||
Username: "testuser",
|
||||
Password: "testpassword",
|
||||
}
|
||||
@@ -26,15 +27,15 @@ func TestAuthServer(t *testing.T) {
|
||||
require.Equal(t, signup.Username, user.Username)
|
||||
}
|
||||
|
||||
func (s *TestingServer) postAuthSignup(signup *api.SignUp) (*api.User, error) {
|
||||
func (s *TestingServer) postAuthSignup(signup *apiv1.SignUp) (*api.User, error) {
|
||||
rawData, err := json.Marshal(&signup)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to marshal signup")
|
||||
}
|
||||
reader := bytes.NewReader(rawData)
|
||||
body, err := s.post("/api/auth/signup", reader, nil)
|
||||
body, err := s.post("/api/v1/auth/signup", reader, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return nil, errors.Wrap(err, "fail to post request")
|
||||
}
|
||||
|
||||
buf := &bytes.Buffer{}
|
||||
@@ -43,12 +44,9 @@ func (s *TestingServer) postAuthSignup(signup *api.SignUp) (*api.User, error) {
|
||||
return nil, errors.Wrap(err, "fail to read response body")
|
||||
}
|
||||
|
||||
type AuthSignupResponse struct {
|
||||
Data *api.User `json:"data"`
|
||||
}
|
||||
res := new(AuthSignupResponse)
|
||||
if err = json.Unmarshal(buf.Bytes(), res); err != nil {
|
||||
user := &api.User{}
|
||||
if err = json.Unmarshal(buf.Bytes(), user); err != nil {
|
||||
return nil, errors.Wrap(err, "fail to unmarshal post signup response")
|
||||
}
|
||||
return res.Data, nil
|
||||
return user, nil
|
||||
}
|
||||
|
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/usememos/memos/api"
|
||||
apiv1 "github.com/usememos/memos/api/v1"
|
||||
)
|
||||
|
||||
func TestMemoRelationServer(t *testing.T) {
|
||||
@@ -18,7 +19,7 @@ func TestMemoRelationServer(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer s.Shutdown(ctx)
|
||||
|
||||
signup := &api.SignUp{
|
||||
signup := &apiv1.SignUp{
|
||||
Username: "testuser",
|
||||
Password: "testpassword",
|
||||
}
|
||||
|
@@ -10,6 +10,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/usememos/memos/api"
|
||||
apiv1 "github.com/usememos/memos/api/v1"
|
||||
)
|
||||
|
||||
func TestMemoServer(t *testing.T) {
|
||||
@@ -18,7 +19,7 @@ func TestMemoServer(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
defer s.Shutdown(ctx)
|
||||
|
||||
signup := &api.SignUp{
|
||||
signup := &apiv1.SignUp{
|
||||
Username: "testuser",
|
||||
Password: "testpassword",
|
||||
}
|
||||
|
@@ -13,6 +13,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/usememos/memos/server"
|
||||
"github.com/usememos/memos/server/profile"
|
||||
"github.com/usememos/memos/store"
|
||||
"github.com/usememos/memos/store/db"
|
||||
"github.com/usememos/memos/test"
|
||||
|
||||
@@ -34,19 +35,19 @@ func NewTestingServer(ctx context.Context, t *testing.T) (*TestingServer, error)
|
||||
return nil, errors.Wrap(err, "failed to open db")
|
||||
}
|
||||
|
||||
server, err := server.NewServer(ctx, profile)
|
||||
store := store.New(db.DBInstance, profile)
|
||||
server, err := server.NewServer(ctx, profile, store)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "failed to create server")
|
||||
}
|
||||
|
||||
errChan := make(chan error, 1)
|
||||
|
||||
s := &TestingServer{
|
||||
server: server,
|
||||
client: &http.Client{},
|
||||
profile: profile,
|
||||
cookie: "",
|
||||
}
|
||||
errChan := make(chan error, 1)
|
||||
|
||||
go func() {
|
||||
if err := s.server.Start(ctx); err != nil {
|
||||
@@ -126,7 +127,7 @@ func (s *TestingServer) request(method, uri string, body io.Reader, params, head
|
||||
}
|
||||
|
||||
if method == "POST" {
|
||||
if strings.Contains(uri, "/api/auth/login") || strings.Contains(uri, "/api/auth/signup") {
|
||||
if strings.Contains(uri, "/api/v1/auth/login") || strings.Contains(uri, "/api/v1/auth/signup") {
|
||||
cookie := ""
|
||||
h := resp.Header.Get("Set-Cookie")
|
||||
parts := strings.Split(h, "; ")
|
||||
@@ -140,7 +141,7 @@ func (s *TestingServer) request(method, uri string, body io.Reader, params, head
|
||||
return nil, errors.Errorf("unable to find access token in the login response headers")
|
||||
}
|
||||
s.cookie = cookie
|
||||
} else if strings.Contains(uri, "/api/auth/logout") {
|
||||
} else if strings.Contains(uri, "/api/v1/auth/logout") {
|
||||
s.cookie = ""
|
||||
}
|
||||
}
|
||||
|
@@ -9,6 +9,7 @@ import (
|
||||
"github.com/pkg/errors"
|
||||
"github.com/stretchr/testify/require"
|
||||
"github.com/usememos/memos/api"
|
||||
apiv1 "github.com/usememos/memos/api/v1"
|
||||
)
|
||||
|
||||
func TestSystemServer(t *testing.T) {
|
||||
@@ -21,7 +22,7 @@ func TestSystemServer(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, (*api.User)(nil), status.Host)
|
||||
|
||||
signup := &api.SignUp{
|
||||
signup := &apiv1.SignUp{
|
||||
Username: "testuser",
|
||||
Password: "testpassword",
|
||||
}
|
||||
|
Reference in New Issue
Block a user