-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
47 additions
and
120 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,11 +3,12 @@ package audit_test | |
import ( | ||
"context" | ||
"errors" | ||
audit2 "github.com/raystack/salt/auth/audit" | ||
"github.com/raystack/salt/auth/audit/mocks" | ||
"testing" | ||
"time" | ||
|
||
"github.com/raystack/salt/auth/audit" | ||
"github.com/raystack/salt/auth/audit/mocks" | ||
|
||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
@@ -18,24 +19,24 @@ type AuditTestSuite struct { | |
now time.Time | ||
|
||
mockRepository *mocks.Repository | ||
service *audit2.Service | ||
service *audit.Service | ||
} | ||
|
||
func (s *AuditTestSuite) setupTest() { | ||
s.mockRepository = new(mocks.Repository) | ||
s.service = audit2.New( | ||
audit2.WithMetadataExtractor(func(context.Context) map[string]interface{} { | ||
s.service = audit.New( | ||
audit.WithMetadataExtractor(func(context.Context) map[string]interface{} { | ||
return map[string]interface{}{ | ||
"trace_id": "test-trace-id", | ||
"app_name": "guardian_test", | ||
"app_version": 1, | ||
} | ||
}), | ||
audit2.WithRepository(s.mockRepository), | ||
audit.WithRepository(s.mockRepository), | ||
) | ||
|
||
s.now = time.Now() | ||
audit2.TimeNow = func() time.Time { | ||
audit.TimeNow = func() time.Time { | ||
return s.now | ||
} | ||
} | ||
|
@@ -48,7 +49,7 @@ func (s *AuditTestSuite) TestLog() { | |
s.Run("should insert to repository", func() { | ||
s.setupTest() | ||
|
||
s.mockRepository.On("Insert", mock.Anything, &audit2.Log{ | ||
s.mockRepository.On("Insert", mock.Anything, &audit.Log{ | ||
Timestamp: s.now, | ||
Action: "action", | ||
Actor: "[email protected]", | ||
|
@@ -61,23 +62,23 @@ func (s *AuditTestSuite) TestLog() { | |
}).Return(nil) | ||
|
||
ctx := context.Background() | ||
ctx = audit2.WithActor(ctx, "[email protected]") | ||
ctx = audit.WithActor(ctx, "[email protected]") | ||
err := s.service.Log(ctx, "action", map[string]interface{}{"foo": "bar"}) | ||
s.NoError(err) | ||
}) | ||
|
||
s.Run("actor extractor", func() { | ||
s.Run("should use actor extractor if option given", func() { | ||
expectedActor := "test-actor" | ||
s.service = audit2.New( | ||
audit2.WithActorExtractor(func(ctx context.Context) (string, error) { | ||
s.service = audit.New( | ||
audit.WithActorExtractor(func(ctx context.Context) (string, error) { | ||
return expectedActor, nil | ||
}), | ||
audit2.WithRepository(s.mockRepository), | ||
audit.WithRepository(s.mockRepository), | ||
) | ||
|
||
s.mockRepository.On("Insert", mock.Anything, mock.Anything).Run(func(args mock.Arguments) { | ||
log := args.Get(1).(*audit2.Log) | ||
log := args.Get(1).(*audit.Log) | ||
s.Equal(expectedActor, log.Actor) | ||
}).Return(nil).Once() | ||
|
||
|
@@ -87,8 +88,8 @@ func (s *AuditTestSuite) TestLog() { | |
|
||
s.Run("should return error if extractor returns error", func() { | ||
expectedError := errors.New("test error") | ||
s.service = audit2.New( | ||
audit2.WithActorExtractor(func(ctx context.Context) (string, error) { | ||
s.service = audit.New( | ||
audit.WithActorExtractor(func(ctx context.Context) (string, error) { | ||
return "", expectedError | ||
}), | ||
) | ||
|
@@ -100,18 +101,18 @@ func (s *AuditTestSuite) TestLog() { | |
|
||
s.Run("metadata", func() { | ||
s.Run("should pass empty trace id if extractor not found", func() { | ||
s.service = audit2.New( | ||
audit2.WithMetadataExtractor(func(ctx context.Context) map[string]interface{} { | ||
s.service = audit.New( | ||
audit.WithMetadataExtractor(func(ctx context.Context) map[string]interface{} { | ||
return map[string]interface{}{ | ||
"app_name": "guardian_test", | ||
"app_version": 1, | ||
} | ||
}), | ||
audit2.WithRepository(s.mockRepository), | ||
audit.WithRepository(s.mockRepository), | ||
) | ||
|
||
s.mockRepository.On("Insert", mock.Anything, mock.Anything).Run(func(args mock.Arguments) { | ||
l := args.Get(1).(*audit2.Log) | ||
l := args.Get(1).(*audit.Log) | ||
s.IsType(map[string]interface{}{}, l.Metadata) | ||
|
||
md := l.Metadata.(map[string]interface{}) | ||
|
@@ -125,25 +126,25 @@ func (s *AuditTestSuite) TestLog() { | |
}) | ||
|
||
s.Run("should append new metadata to existing one", func() { | ||
s.service = audit2.New( | ||
audit2.WithMetadataExtractor(func(ctx context.Context) map[string]interface{} { | ||
s.service = audit.New( | ||
audit.WithMetadataExtractor(func(ctx context.Context) map[string]interface{} { | ||
return map[string]interface{}{ | ||
"existing": "foobar", | ||
} | ||
}), | ||
audit2.WithRepository(s.mockRepository), | ||
audit.WithRepository(s.mockRepository), | ||
) | ||
|
||
expectedMetadata := map[string]interface{}{ | ||
"existing": "foobar", | ||
"new": "foobar", | ||
} | ||
s.mockRepository.On("Insert", mock.Anything, mock.Anything).Run(func(args mock.Arguments) { | ||
log := args.Get(1).(*audit2.Log) | ||
log := args.Get(1).(*audit.Log) | ||
s.Equal(expectedMetadata, log.Metadata) | ||
}).Return(nil).Once() | ||
|
||
ctx, err := audit2.WithMetadata(context.Background(), map[string]interface{}{ | ||
ctx, err := audit.WithMetadata(context.Background(), map[string]interface{}{ | ||
"new": "foobar", | ||
}) | ||
s.Require().NoError(err) | ||
|
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
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
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
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
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
Oops, something went wrong.