2023-04-17 16:34:59 +03:00
|
|
|
package testserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-09-17 17:55:13 +03:00
|
|
|
|
2023-06-17 16:25:46 +03:00
|
|
|
apiv1 "github.com/usememos/memos/api/v1"
|
2023-04-17 16:34:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAuthServer(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
s, err := NewTestingServer(ctx, t)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer s.Shutdown(ctx)
|
|
|
|
|
2023-06-17 16:25:46 +03:00
|
|
|
signup := &apiv1.SignUp{
|
2023-04-17 16:34:59 +03:00
|
|
|
Username: "testuser",
|
|
|
|
Password: "testpassword",
|
|
|
|
}
|
2023-08-10 04:01:38 +03:00
|
|
|
user, err := s.postAuthSignUp(signup)
|
2023-04-17 16:34:59 +03:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, signup.Username, user.Username)
|
2023-08-10 04:01:38 +03:00
|
|
|
|
|
|
|
signin := &apiv1.SignIn{
|
|
|
|
Username: "testuser",
|
|
|
|
Password: "testpassword",
|
|
|
|
}
|
|
|
|
user, err = s.postAuthSignIn(signin)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, signup.Username, user.Username)
|
|
|
|
err = s.postSignOut()
|
|
|
|
require.NoError(t, err)
|
|
|
|
_, err = s.getCurrentUser()
|
|
|
|
require.Error(t, err)
|
2023-04-17 16:34:59 +03:00
|
|
|
}
|
|
|
|
|
2023-08-10 04:01:38 +03:00
|
|
|
func (s *TestingServer) postAuthSignUp(signup *apiv1.SignUp) (*apiv1.User, error) {
|
2023-04-17 16:34:59 +03:00
|
|
|
rawData, err := json.Marshal(&signup)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to marshal signup")
|
|
|
|
}
|
|
|
|
reader := bytes.NewReader(rawData)
|
2023-06-17 16:25:46 +03:00
|
|
|
body, err := s.post("/api/v1/auth/signup", reader, nil)
|
2023-04-17 16:34:59 +03:00
|
|
|
if err != nil {
|
2023-06-17 16:25:46 +03:00
|
|
|
return nil, errors.Wrap(err, "fail to post request")
|
2023-04-17 16:34:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
_, err = buf.ReadFrom(body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "fail to read response body")
|
|
|
|
}
|
|
|
|
|
2023-07-02 13:56:25 +03:00
|
|
|
user := &apiv1.User{}
|
2023-06-17 16:25:46 +03:00
|
|
|
if err = json.Unmarshal(buf.Bytes(), user); err != nil {
|
2023-04-17 16:34:59 +03:00
|
|
|
return nil, errors.Wrap(err, "fail to unmarshal post signup response")
|
|
|
|
}
|
2023-06-17 16:25:46 +03:00
|
|
|
return user, nil
|
2023-04-17 16:34:59 +03:00
|
|
|
}
|
2023-08-10 04:01:38 +03:00
|
|
|
|
|
|
|
func (s *TestingServer) postAuthSignIn(signip *apiv1.SignIn) (*apiv1.User, error) {
|
|
|
|
rawData, err := json.Marshal(&signip)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to marshal signin")
|
|
|
|
}
|
|
|
|
reader := bytes.NewReader(rawData)
|
|
|
|
body, err := s.post("/api/v1/auth/signin", reader, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "fail to post request")
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
_, err = buf.ReadFrom(body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "fail to read response body")
|
|
|
|
}
|
|
|
|
|
|
|
|
user := &apiv1.User{}
|
|
|
|
if err = json.Unmarshal(buf.Bytes(), user); err != nil {
|
|
|
|
return nil, errors.Wrap(err, "fail to unmarshal post signin response")
|
|
|
|
}
|
|
|
|
return user, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *TestingServer) postSignOut() error {
|
|
|
|
_, err := s.post("/api/v1/auth/signout", nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "fail to post request")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|