NOISSUE - Update auth in journal service (#2527)

Signed-off-by: Felix Gateru <felix.gateru@gmail.com>
This commit is contained in:
Felix Gateru
2024-11-25 15:02:27 +03:00
committed by GitHub
parent f42f45e305
commit b20b45023d
22 changed files with 593 additions and 270 deletions
+7 -2
View File
@@ -30,7 +30,7 @@ type JournalsPage struct {
Journals []Journal `json:"journals"`
}
func (sdk mgSDK) Journal(entityType, entityID string, pm PageMetadata, token string) (journals JournalsPage, err error) {
func (sdk mgSDK) Journal(entityType, entityID, domainID string, pm PageMetadata, token string) (journals JournalsPage, err error) {
if entityID == "" {
return JournalsPage{}, errors.NewSDKError(apiutil.ErrMissingID)
}
@@ -38,7 +38,12 @@ func (sdk mgSDK) Journal(entityType, entityID string, pm PageMetadata, token str
return JournalsPage{}, errors.NewSDKError(apiutil.ErrMissingEntityType)
}
url, err := sdk.withQueryParams(sdk.journalURL, fmt.Sprintf("%s/%s/%s", journalEndpoint, entityType, entityID), pm)
reqUrl := fmt.Sprintf("%s/%s/%s/%s", domainID, journalEndpoint, entityType, entityID)
if entityType == "user" {
reqUrl = fmt.Sprintf("%s/%s/%s", journalEndpoint, entityType, entityID)
}
url, err := sdk.withQueryParams(sdk.journalURL, reqUrl, pm)
if err != nil {
return JournalsPage{}, errors.NewSDKError(err)
}
+117 -15
View File
@@ -15,6 +15,8 @@ import (
"github.com/absmach/magistrala/journal/mocks"
mglog "github.com/absmach/magistrala/logger"
"github.com/absmach/magistrala/pkg/apiutil"
mgauthn "github.com/absmach/magistrala/pkg/authn"
authnmocks "github.com/absmach/magistrala/pkg/authn/mocks"
"github.com/absmach/magistrala/pkg/errors"
svcerr "github.com/absmach/magistrala/pkg/errors/service"
sdk "github.com/absmach/magistrala/pkg/sdk/go"
@@ -22,20 +24,21 @@ import (
"github.com/stretchr/testify/mock"
)
func setupJournal() (*httptest.Server, *mocks.Service) {
func setupJournal() (*httptest.Server, *mocks.Service, *authnmocks.Authentication) {
svc := new(mocks.Service)
authn := new(authnmocks.Authentication)
logger := mglog.NewMock()
mux := api.MakeHandler(svc, logger, "journal-log", "test")
return httptest.NewServer(mux), svc
mux := api.MakeHandler(svc, authn, logger, "journal-log", "test")
return httptest.NewServer(mux), svc, authn
}
func TestRetrieveJournal(t *testing.T) {
js, svc := setupJournal()
js, svc, authn := setupJournal()
defer js.Close()
testJournal := generateTestJournal(t)
validEntityType := "user"
validEntityType := "group"
sdkConf := sdk.Config{
JournalURL: js.URL,
@@ -46,20 +49,24 @@ func TestRetrieveJournal(t *testing.T) {
cases := []struct {
desc string
token string
session mgauthn.Session
entityType string
entityID string
domainID string
pageMeta sdk.PageMetadata
svcReq journal.Page
svcRes journal.JournalsPage
svcErr error
authnErr error
response sdk.JournalsPage
err error
}{
{
desc: "retrieve journal successfully",
desc: "retrieve user journal successfully",
token: validToken,
entityType: validEntityType,
entityType: "user",
entityID: validID,
domainID: domainID,
pageMeta: sdk.PageMetadata{
Offset: 0,
Limit: 10,
@@ -82,6 +89,90 @@ func TestRetrieveJournal(t *testing.T) {
},
err: nil,
},
{
desc: "retrieve channel journal successfully",
token: validToken,
entityType: "channel",
entityID: validID,
domainID: domainID,
pageMeta: sdk.PageMetadata{
Offset: 0,
Limit: 10,
},
svcReq: journal.Page{
Offset: 0,
Limit: 10,
EntityID: validID,
EntityType: journal.ChannelEntity,
Direction: "desc",
},
svcRes: journal.JournalsPage{
Total: 1,
Journals: []journal.Journal{convertJournal(testJournal)},
},
svcErr: nil,
response: sdk.JournalsPage{
Total: 1,
Journals: []sdk.Journal{testJournal},
},
err: nil,
},
{
desc: "retrieve group journal successfully",
token: validToken,
entityType: "group",
entityID: validID,
domainID: domainID,
pageMeta: sdk.PageMetadata{
Offset: 0,
Limit: 10,
},
svcReq: journal.Page{
Offset: 0,
Limit: 10,
EntityID: validID,
EntityType: journal.GroupEntity,
Direction: "desc",
},
svcRes: journal.JournalsPage{
Total: 1,
Journals: []journal.Journal{convertJournal(testJournal)},
},
svcErr: nil,
response: sdk.JournalsPage{
Total: 1,
Journals: []sdk.Journal{testJournal},
},
err: nil,
},
{
desc: "retrieve thing journal successfully",
token: validToken,
entityType: "thing",
entityID: validID,
domainID: domainID,
pageMeta: sdk.PageMetadata{
Offset: 0,
Limit: 10,
},
svcReq: journal.Page{
Offset: 0,
Limit: 10,
EntityID: validID,
EntityType: journal.ThingEntity,
Direction: "desc",
},
svcRes: journal.JournalsPage{
Total: 1,
Journals: []journal.Journal{convertJournal(testJournal)},
},
svcErr: nil,
response: sdk.JournalsPage{
Total: 1,
Journals: []sdk.Journal{testJournal},
},
err: nil,
},
{
desc: "retrieve journal with invalid token",
token: invalidToken,
@@ -95,11 +186,11 @@ func TestRetrieveJournal(t *testing.T) {
Offset: 0,
Limit: 10,
EntityID: validID,
EntityType: journal.UserEntity,
EntityType: journal.GroupEntity,
Direction: "desc",
},
svcRes: journal.JournalsPage{},
svcErr: svcerr.ErrAuthentication,
authnErr: svcerr.ErrAuthentication,
response: sdk.JournalsPage{},
err: errors.NewSDKErrorWithStatus(svcerr.ErrAuthentication, http.StatusUnauthorized),
},
@@ -116,13 +207,14 @@ func TestRetrieveJournal(t *testing.T) {
svcRes: journal.JournalsPage{},
svcErr: nil,
response: sdk.JournalsPage{},
err: errors.NewSDKErrorWithStatus(errors.Wrap(apiutil.ErrValidation, apiutil.ErrBearerToken), http.StatusUnauthorized),
err: errors.NewSDKErrorWithStatus(apiutil.ErrBearerToken, http.StatusUnauthorized),
},
{
desc: "retrieve journal with invalid entity type",
token: validToken,
entityType: "invalid",
entityID: validID,
domainID: domainID,
pageMeta: sdk.PageMetadata{
Offset: 0,
Limit: 10,
@@ -138,6 +230,7 @@ func TestRetrieveJournal(t *testing.T) {
token: validToken,
entityType: validEntityType,
entityID: "",
domainID: domainID,
pageMeta: sdk.PageMetadata{
Offset: 0,
Limit: 10,
@@ -153,6 +246,7 @@ func TestRetrieveJournal(t *testing.T) {
token: validToken,
entityType: "",
entityID: validID,
domainID: domainID,
pageMeta: sdk.PageMetadata{
Offset: 0,
Limit: 10,
@@ -168,6 +262,7 @@ func TestRetrieveJournal(t *testing.T) {
token: validToken,
entityType: validEntityType,
entityID: validID,
domainID: domainID,
pageMeta: sdk.PageMetadata{
Offset: 0,
Limit: 1000,
@@ -183,6 +278,7 @@ func TestRetrieveJournal(t *testing.T) {
token: validToken,
entityType: validEntityType,
entityID: validID,
domainID: domainID,
pageMeta: sdk.PageMetadata{
Offset: 0,
Limit: 10,
@@ -201,6 +297,7 @@ func TestRetrieveJournal(t *testing.T) {
token: validToken,
entityType: validEntityType,
entityID: validID,
domainID: domainID,
pageMeta: sdk.PageMetadata{
Offset: 0,
Limit: 10,
@@ -209,7 +306,7 @@ func TestRetrieveJournal(t *testing.T) {
Offset: 0,
Limit: 10,
EntityID: validID,
EntityType: journal.UserEntity,
EntityType: journal.GroupEntity,
Direction: "desc",
},
svcRes: journal.JournalsPage{
@@ -231,15 +328,20 @@ func TestRetrieveJournal(t *testing.T) {
}
for _, tc := range cases {
t.Run(tc.desc, func(t *testing.T) {
svcCall := svc.On("RetrieveAll", mock.Anything, tc.token, tc.svcReq).Return(tc.svcRes, tc.svcErr)
resp, err := mgsdk.Journal(tc.entityType, tc.entityID, tc.pageMeta, tc.token)
if tc.token == validToken {
tc.session = mgauthn.Session{DomainUserID: domainID + "_" + validID, UserID: validID, DomainID: domainID}
}
authCall := authn.On("Authenticate", mock.Anything, mock.Anything).Return(tc.session, tc.authnErr)
svcCall := svc.On("RetrieveAll", mock.Anything, tc.session, tc.svcReq).Return(tc.svcRes, tc.svcErr)
resp, err := mgsdk.Journal(tc.entityType, tc.entityID, tc.domainID, tc.pageMeta, tc.token)
assert.Equal(t, tc.err, err)
assert.Equal(t, tc.response, resp)
if tc.err == nil {
ok := svcCall.Parent.AssertCalled(t, "RetrieveAll", mock.Anything, tc.token, tc.svcReq)
ok := svcCall.Parent.AssertCalled(t, "RetrieveAll", mock.Anything, tc.session, tc.svcReq)
assert.True(t, ok)
}
svcCall.Unset()
authCall.Unset()
})
}
}
+2 -2
View File
@@ -1210,9 +1210,9 @@ type SDK interface {
// Journal returns a list of journal logs.
//
// For example:
// journals, _ := sdk.Journal("thing", "thingID", PageMetadata{Offset: 0, Limit: 10, Operation: "users.create"}, "token")
// journals, _ := sdk.Journal("thing", "thingID","domainID", PageMetadata{Offset: 0, Limit: 10, Operation: "thing.create"}, "token")
// fmt.Println(journals)
Journal(entityType, entityID string, pm PageMetadata, token string) (journal JournalsPage, err error)
Journal(entityType, entityID, domainID string, pm PageMetadata, token string) (journal JournalsPage, err error)
}
type mgSDK struct {
+9 -9
View File
@@ -1402,9 +1402,9 @@ func (_m *SDK) IssueCert(thingID string, validity string, domainID string, token
return r0, r1
}
// Journal provides a mock function with given fields: entityType, entityID, pm, token
func (_m *SDK) Journal(entityType string, entityID string, pm sdk.PageMetadata, token string) (sdk.JournalsPage, error) {
ret := _m.Called(entityType, entityID, pm, token)
// Journal provides a mock function with given fields: entityType, entityID, domainID, pm, token
func (_m *SDK) Journal(entityType string, entityID string, domainID string, pm sdk.PageMetadata, token string) (sdk.JournalsPage, error) {
ret := _m.Called(entityType, entityID, domainID, pm, token)
if len(ret) == 0 {
panic("no return value specified for Journal")
@@ -1412,17 +1412,17 @@ func (_m *SDK) Journal(entityType string, entityID string, pm sdk.PageMetadata,
var r0 sdk.JournalsPage
var r1 error
if rf, ok := ret.Get(0).(func(string, string, sdk.PageMetadata, string) (sdk.JournalsPage, error)); ok {
return rf(entityType, entityID, pm, token)
if rf, ok := ret.Get(0).(func(string, string, string, sdk.PageMetadata, string) (sdk.JournalsPage, error)); ok {
return rf(entityType, entityID, domainID, pm, token)
}
if rf, ok := ret.Get(0).(func(string, string, sdk.PageMetadata, string) sdk.JournalsPage); ok {
r0 = rf(entityType, entityID, pm, token)
if rf, ok := ret.Get(0).(func(string, string, string, sdk.PageMetadata, string) sdk.JournalsPage); ok {
r0 = rf(entityType, entityID, domainID, pm, token)
} else {
r0 = ret.Get(0).(sdk.JournalsPage)
}
if rf, ok := ret.Get(1).(func(string, string, sdk.PageMetadata, string) error); ok {
r1 = rf(entityType, entityID, pm, token)
if rf, ok := ret.Get(1).(func(string, string, string, sdk.PageMetadata, string) error); ok {
r1 = rf(entityType, entityID, domainID, pm, token)
} else {
r1 = ret.Error(1)
}