Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: http and route #198

Merged
merged 33 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
5572bb8
refactor: route and http
devhaozi Jun 19, 2023
6bee76f
fix: route and http lint
devhaozi Jun 19, 2023
f617627
fix: lint
devhaozi Jun 19, 2023
d25b994
Merge branch 'master' into haozi-gin
devhaozi Jun 19, 2023
3cd314b
fix: add limit file
devhaozi Jun 19, 2023
80dc150
Merge remote-tracking branch 'origin/haozi-gin' into haozi-gin
devhaozi Jun 19, 2023
2cbd18b
feat: remove gin
devhaozi Jun 19, 2023
b4a7e90
feat: add Background
devhaozi Jun 19, 2023
78894e0
Merge remote-tracking branch 'origin/master' into haozi-gin
devhaozi Jun 30, 2023
52e0764
fix(auth): tests
devhaozi Jun 30, 2023
54ecced
fix(foundation): tests
devhaozi Jun 30, 2023
c4de5da
feat(route): delete tests
devhaozi Jun 30, 2023
a041880
feat: optimize code
devhaozi Jun 30, 2023
a221348
Merge remote-tracking branch 'origin/master' into haozi-gin
devhaozi Jul 9, 2023
eb6484d
feat: remove
devhaozi Jul 9, 2023
9413ec8
feat: generate mock
devhaozi Jul 9, 2023
8e6ac13
Merge remote-tracking branch 'origin/master' into haozi-gin
devhaozi Aug 10, 2023
850cce6
feat: update
devhaozi Aug 10, 2023
4b96258
Merge remote-tracking branch 'origin/master' into haozi-gin
devhaozi Aug 10, 2023
e8643d4
fix(auth): tests
devhaozi Aug 10, 2023
180a626
refactor(http): writer and flusher
devhaozi Aug 10, 2023
0ec25cb
refactor(http): writer and flusher
devhaozi Aug 11, 2023
b9b8102
refactor(http): writer and flusher
devhaozi Aug 11, 2023
76807c1
Merge remote-tracking branch 'origin/master' into haozi-gin
devhaozi Aug 11, 2023
fe775fe
fix: remove unnecessary struct
devhaozi Aug 11, 2023
bdc55fe
feat: remove RateLimiter
devhaozi Aug 11, 2023
28327f9
Revert "feat: remove RateLimiter"
devhaozi Aug 11, 2023
9795919
revert: http and route
devhaozi Aug 11, 2023
c328850
fix: default driver
devhaozi Aug 11, 2023
7ca313e
feat: go mod tidy
devhaozi Aug 11, 2023
d093a94
feat: remove http context
devhaozi Aug 12, 2023
9356bce
fix: remove unnecessary value
devhaozi Aug 12, 2023
387f600
Merge branch 'master' into haozi-gin
devhaozi Aug 16, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 89 additions & 26 deletions auth/auth_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package auth

import (
"context"
"errors"
"sync"
"testing"
"time"

Expand All @@ -14,8 +16,8 @@ import (
cachemock "github.com/goravel/framework/contracts/cache/mocks"
configmock "github.com/goravel/framework/contracts/config/mocks"
ormmock "github.com/goravel/framework/contracts/database/orm/mocks"
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/database/orm"
"github.com/goravel/framework/http"
"github.com/goravel/framework/support/carbon"
)

Expand All @@ -26,6 +28,67 @@ type User struct {
Name string
}

type Context struct {
ctx context.Context
request http.Request
response http.Response
values map[string]any
mu sync.RWMutex
}

func (mc *Context) Deadline() (deadline time.Time, ok bool) {
return mc.ctx.Deadline()
}

func (mc *Context) Done() <-chan struct{} {
return mc.ctx.Done()
}

func (mc *Context) Err() error {
return mc.ctx.Err()
}

func (mc *Context) Value(key interface{}) any {
if k, ok := key.(string); ok {
mc.mu.RLock()
v, ok := mc.values[k]
mc.mu.RUnlock()

if ok {
return v
}
}

return mc.ctx.Value(key)
}

func (mc *Context) Context() context.Context {
return mc.ctx
}

func (mc *Context) WithValue(key string, value any) {
mc.mu.Lock()
mc.values[key] = value
mc.mu.Unlock()
}

func (mc *Context) Request() http.Request {
return mc.request
}

func (mc *Context) Response() http.Response {
return mc.response
}

func Background() http.Context {
return &Context{
ctx: context.Background(),
request: nil,
response: nil,
values: make(map[string]any),
}
}

type AuthTestSuite struct {
suite.Suite
auth *Auth
Expand All @@ -50,7 +113,7 @@ func (s *AuthTestSuite) SetupTest() {
func (s *AuthTestSuite) TestLoginUsingID_EmptySecret() {
s.mockConfig.On("GetString", "jwt.secret").Return("").Once()

token, err := s.auth.LoginUsingID(http.Background(), 1)
token, err := s.auth.LoginUsingID(Background(), 1)
s.Empty(token)
s.ErrorIs(err, ErrorEmptySecret)

Expand All @@ -61,7 +124,7 @@ func (s *AuthTestSuite) TestLoginUsingID_InvalidKey() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Once()
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Once()

token, err := s.auth.LoginUsingID(http.Background(), "")
token, err := s.auth.LoginUsingID(Background(), "")
s.Empty(token)
s.ErrorIs(err, ErrorInvalidKey)

Expand All @@ -73,7 +136,7 @@ func (s *AuthTestSuite) TestLoginUsingID() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Once()
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Once()

token, err := s.auth.LoginUsingID(http.Background(), 1)
token, err := s.auth.LoginUsingID(Background(), 1)
s.NotEmpty(token)
s.Nil(err)

Expand All @@ -88,7 +151,7 @@ func (s *AuthTestSuite) TestLogin_Model() {
var user User
user.ID = 1
user.Name = "Goravel"
token, err := s.auth.Login(http.Background(), &user)
token, err := s.auth.Login(Background(), &user)
s.NotEmpty(token)
s.Nil(err)

Expand All @@ -107,7 +170,7 @@ func (s *AuthTestSuite) TestLogin_CustomModel() {
var user CustomUser
user.ID = 1
user.Name = "Goravel"
token, err := s.auth.Login(http.Background(), &user)
token, err := s.auth.Login(Background(), &user)
s.NotEmpty(token)
s.Nil(err)

Expand All @@ -123,7 +186,7 @@ func (s *AuthTestSuite) TestLogin_ErrorModel() {
var errorUser ErrorUser
errorUser.ID = 1
errorUser.Name = "Goravel"
token, err := s.auth.Login(http.Background(), &errorUser)
token, err := s.auth.Login(Background(), &errorUser)
s.Empty(token)
s.EqualError(err, "the primaryKey field was not found in the model, set primaryKey like orm.Model")
}
Expand All @@ -134,7 +197,7 @@ func (s *AuthTestSuite) TestLogin_NoPrimaryKey() {
Name string
}

ctx := http.Background()
ctx := Background()
var user User
user.ID = 1
user.Name = "Goravel"
Expand All @@ -147,7 +210,7 @@ func (s *AuthTestSuite) TestParse_TokenDisabled() {
token := "1"
s.mockCache.On("GetBool", "jwt:disabled:"+token, false).Return(true).Once()

payload, err := s.auth.Parse(http.Background(), token)
payload, err := s.auth.Parse(Background(), token)
s.Nil(payload)
s.EqualError(err, "token is disabled")
}
Expand All @@ -159,7 +222,7 @@ func (s *AuthTestSuite) TestParse_TokenInvalid() {
token := "1"
s.mockCache.On("GetBool", "jwt:disabled:"+token, false).Return(false).Once()

payload, err := s.auth.Parse(http.Background(), token)
payload, err := s.auth.Parse(Background(), token)
s.Nil(payload)
s.NotNil(err)

Expand All @@ -171,7 +234,7 @@ func (s *AuthTestSuite) TestParse_TokenExpired() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Twice()
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Once()

ctx := http.Background()
ctx := Background()

now := carbon.Now()
issuedAt := now.ToStdTime()
Expand Down Expand Up @@ -199,7 +262,7 @@ func (s *AuthTestSuite) TestParse_TokenExpired() {

func (s *AuthTestSuite) TestParse_InvalidCache() {
auth := NewAuth(guard, nil, s.mockConfig, s.mockOrm)
ctx := http.Background()
ctx := Background()
payload, err := auth.Parse(ctx, "1")
s.Nil(payload)
s.EqualError(err, "cache support is required")
Expand All @@ -209,7 +272,7 @@ func (s *AuthTestSuite) TestParse_Success() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Twice()
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Once()

ctx := http.Background()
ctx := Background()
token, err := s.auth.LoginUsingID(ctx, 1)
s.Nil(err)

Expand All @@ -231,7 +294,7 @@ func (s *AuthTestSuite) TestParse_SuccessWithPrefix() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Twice()
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Once()

ctx := http.Background()
ctx := Background()
token, err := s.auth.LoginUsingID(ctx, 1)
s.Nil(err)

Expand All @@ -250,7 +313,7 @@ func (s *AuthTestSuite) TestParse_SuccessWithPrefix() {
}

func (s *AuthTestSuite) TestUser_NoParse() {
ctx := http.Background()
ctx := Background()
var user User
err := s.auth.User(ctx, user)
s.EqualError(err, "parse token first")
Expand All @@ -262,7 +325,7 @@ func (s *AuthTestSuite) TestUser_DBError() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Twice()
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Once()

ctx := http.Background()
ctx := Background()
token, err := s.auth.LoginUsingID(ctx, 1)
s.Nil(err)

Expand All @@ -287,7 +350,7 @@ func (s *AuthTestSuite) TestUser_Expired() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Times(3)
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Twice()

ctx := http.Background()
ctx := Background()
token, err := s.auth.LoginUsingID(ctx, 1)
s.NotEmpty(token)
s.Nil(err)
Expand Down Expand Up @@ -325,7 +388,7 @@ func (s *AuthTestSuite) TestUser_RefreshExpired() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Twice()
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Once()

ctx := http.Background()
ctx := Background()
token, err := s.auth.LoginUsingID(ctx, 1)
s.NotEmpty(token)
s.Nil(err)
Expand Down Expand Up @@ -359,7 +422,7 @@ func (s *AuthTestSuite) TestUser_Success() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Twice()
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Once()

ctx := http.Background()
ctx := Background()
token, err := s.auth.LoginUsingID(ctx, 1)
s.Nil(err)

Expand All @@ -380,7 +443,7 @@ func (s *AuthTestSuite) TestUser_Success() {
}

func (s *AuthTestSuite) TestRefresh_NotParse() {
ctx := http.Background()
ctx := Background()
token, err := s.auth.Refresh(ctx)
s.Empty(token)
s.EqualError(err, "parse token first")
Expand All @@ -392,7 +455,7 @@ func (s *AuthTestSuite) TestRefresh_RefreshTimeExceeded() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Twice()
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Once()

ctx := http.Background()
ctx := Background()
token, err := s.auth.LoginUsingID(ctx, 2)

s.Nil(err)
Expand Down Expand Up @@ -420,7 +483,7 @@ func (s *AuthTestSuite) TestRefresh_Success() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Times(3)
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Twice()

ctx := http.Background()
ctx := Background()
token, err := s.auth.LoginUsingID(ctx, 1)
s.Nil(err)

Expand Down Expand Up @@ -448,7 +511,7 @@ func (s *AuthTestSuite) TestLogout_CacheUnsupported() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Once()
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Once()

ctx := http.Background()
ctx := Background()
token, err := s.auth.LoginUsingID(ctx, 1)
s.NotEmpty(token)
s.Nil(err)
Expand All @@ -458,14 +521,14 @@ func (s *AuthTestSuite) TestLogout_CacheUnsupported() {
}

func (s *AuthTestSuite) TestLogout_NotParse() {
s.Nil(s.auth.Logout(http.Background()))
s.Nil(s.auth.Logout(Background()))
}

func (s *AuthTestSuite) TestLogout_SetDisabledCacheError() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Twice()
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Twice()

ctx := http.Background()
ctx := Background()
token, err := s.auth.LoginUsingID(ctx, 1)
s.Nil(err)

Expand All @@ -486,7 +549,7 @@ func (s *AuthTestSuite) TestLogout_Success() {
s.mockConfig.On("GetString", "jwt.secret").Return("Goravel").Twice()
s.mockConfig.On("GetInt", "jwt.ttl").Return(2).Twice()

ctx := http.Background()
ctx := Background()
token, err := s.auth.LoginUsingID(ctx, 1)
s.NotEmpty(token)
s.Nil(err)
Expand Down
18 changes: 9 additions & 9 deletions filesystem/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,9 @@ import (
"bytes"
"mime/multipart"
"net/http"
"net/http/httptest"
"path/filepath"
"testing"

"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"

Expand Down Expand Up @@ -77,13 +75,15 @@ func TestNewFileFromRequest(t *testing.T) {
assert.NoError(t, err)
}
assert.Nil(t, mw.Close())
c, _ := gin.CreateTestContext(httptest.NewRecorder())
c.Request, _ = http.NewRequest("POST", "/", buf)
c.Request.Header.Set("Content-Type", mw.FormDataContentType())
f, err := c.FormFile("file")
assert.Nil(t, err)
requestFile, err := NewFileFromRequest(f)
assert.Nil(t, err)
req, err := http.NewRequest("POST", "/", buf)
assert.NoError(t, err)
req.Header.Set("Content-Type", mw.FormDataContentType())
err = req.ParseMultipartForm(10 << 20) // 10 MB
assert.NoError(t, err)
_, fileHeader, err := req.FormFile("file")
assert.NoError(t, err)
requestFile, err := NewFileFromRequest(fileHeader)
assert.NoError(t, err)
assert.Equal(t, ".txt", filepath.Ext(requestFile.path))

mockConfig.AssertExpectations(t)
Expand Down
8 changes: 7 additions & 1 deletion foundation/application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/goravel/framework/contracts/foundation"
logmocks "github.com/goravel/framework/contracts/log/mocks"
queuemocks "github.com/goravel/framework/contracts/queue/mocks"
contractsroute "github.com/goravel/framework/contracts/route"
"github.com/goravel/framework/crypt"
"github.com/goravel/framework/database"
"github.com/goravel/framework/database/gorm"
Expand Down Expand Up @@ -311,9 +312,14 @@ func (s *ApplicationTestSuite) TestMakeRateLimiter() {
s.NotNil(s.app.MakeRateLimiter())
}

type Engine struct {
devhaozi marked this conversation as resolved.
Show resolved Hide resolved
contractsroute.Engine
hwbrzzl marked this conversation as resolved.
Show resolved Hide resolved
}

func (s *ApplicationTestSuite) TestMakeRoute() {
mockConfig := &configmocks.Config{}
mockConfig.On("GetBool", "app.debug").Return(false).Once()
mockConfig.On("GetString", "http.driver").Return("gin").Once()
mockConfig.On("Get", "http.drivers.gin.route").Return(&Engine{}).Once()

s.app.Singleton(config.Binding, func(app foundation.Application) (any, error) {
return mockConfig, nil
Expand Down
Loading