Files
opencloud/services/notifications/pkg/service/service_test.go
T
2026-11-17 12:45:18 +01:00

560 lines
19 KiB
Go

package service_test
import (
"context"
"time"
settingsmsg "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/messages/settings/v0"
"github.com/opencloud-eu/reva/v3/pkg/store"
gateway "github.com/cs3org/go-cs3apis/cs3/gateway/v1beta1"
user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
rpc "github.com/cs3org/go-cs3apis/cs3/rpc/v1beta1"
provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
ocEvents "github.com/opencloud-eu/opencloud/pkg/events"
"github.com/opencloud-eu/opencloud/pkg/log"
"github.com/opencloud-eu/opencloud/pkg/shared"
settingssvc "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/settings/v0"
settingsmocks "github.com/opencloud-eu/opencloud/protogen/gen/opencloud/services/settings/v0/mocks"
"github.com/opencloud-eu/opencloud/services/graph/pkg/config/defaults"
"github.com/opencloud-eu/opencloud/services/notifications/pkg/channels"
"github.com/opencloud-eu/opencloud/services/notifications/pkg/service"
"github.com/opencloud-eu/reva/v3/pkg/events"
"github.com/opencloud-eu/reva/v3/pkg/rgrpc/todo/pool"
"github.com/opencloud-eu/reva/v3/pkg/utils"
cs3mocks "github.com/opencloud-eu/reva/v3/tests/cs3mocks/mocks"
"github.com/stretchr/testify/mock"
"google.golang.org/grpc"
)
var _ = Describe("Notifications", func() {
var (
gatewayClient *cs3mocks.GatewayAPIClient
gatewaySelector pool.Selectable[gateway.GatewayAPIClient]
vs *settingsmocks.ValueService
sharer = &user.User{
Id: &user.UserId{
OpaqueId: "sharer",
},
Mail: "sharer@opencloud.eu",
DisplayName: "Dr. S. Harer",
}
sharee = &user.User{
Id: &user.UserId{
OpaqueId: "sharee",
},
Mail: "sharee@opencloud.eu",
DisplayName: "Eric Expireling",
}
resourceid = &provider.ResourceId{
StorageId: "storageid",
SpaceId: "spaceid",
OpaqueId: "itemid",
}
)
BeforeEach(func() {
pool.RemoveSelector("GatewaySelector" + "eu.opencloud.api.gateway")
gatewayClient = &cs3mocks.GatewayAPIClient{}
gatewaySelector = pool.GetSelector[gateway.GatewayAPIClient](
"GatewaySelector",
"eu.opencloud.api.gateway",
func(cc grpc.ClientConnInterface) gateway.GatewayAPIClient {
return gatewayClient
},
)
gatewayClient.On("GetUser", mock.Anything, mock.MatchedBy(func(req *user.GetUserRequest) bool {
return req.GetUserId().GetOpaqueId() == sharer.GetId().GetOpaqueId()
})).Return(&user.GetUserResponse{Status: &rpc.Status{Code: rpc.Code_CODE_OK}, User: sharer}, nil)
gatewayClient.On("GetUser", mock.Anything, mock.MatchedBy(func(req *user.GetUserRequest) bool {
return req.GetUserId().GetOpaqueId() == sharee.GetId().GetOpaqueId()
})).Return(&user.GetUserResponse{Status: &rpc.Status{Code: rpc.Code_CODE_OK}, User: sharee}, nil)
gatewayClient.On("Authenticate", mock.Anything, mock.Anything).Return(&gateway.AuthenticateResponse{Status: &rpc.Status{Code: rpc.Code_CODE_OK}, User: sharer}, nil)
gatewayClient.On("Stat", mock.Anything, mock.Anything).Return(&provider.StatResponse{Status: &rpc.Status{Code: rpc.Code_CODE_OK}, Info: &provider.ResourceInfo{Name: "secrets of the board", Space: &provider.StorageSpace{Name: "secret space"}}}, nil)
vs = &settingsmocks.ValueService{}
vs.On("GetValueByUniqueIdentifiers", mock.Anything, mock.Anything).Return(&settingssvc.GetValueResponse{
Value: &settingsmsg.ValueWithIdentifier{
Value: &settingsmsg.Value{
Value: &settingsmsg.Value_CollectionValue{
CollectionValue: &settingsmsg.CollectionValue{
Values: []*settingsmsg.CollectionOption{
{
Key: "mail",
Option: &settingsmsg.CollectionOption_BoolValue{BoolValue: true},
},
},
},
},
},
},
}, nil)
})
DescribeTable("Sending userEventIds",
func(tc testChannel, ev events.Event) {
cfg := defaults.FullDefaultConfig()
cfg.GRPCClientTLS = &shared.GRPCClientTLS{}
ch := make(chan events.Event)
evts := service.NewEventsNotifier(ch, tc, log.NewLogger(), gatewaySelector, vs, "",
"", "", "", testOpenCloudURL, "", "",
store.Create(), nil, nil)
go evts.Run()
ch <- ev
select {
case <-tc.done:
// finished
case <-time.Tick(3 * time.Second):
Fail("timeout waiting for notification")
}
},
Entry("Share Created", testChannel{
expectedReceipients: []string{sharee.GetMail()},
expectedSubject: "Dr. S. Harer shared 'secrets of the board' with you",
expectedTextBody: `Hello Eric Expireling
Dr. S. Harer has shared "secrets of the board" with you.
Click here to view it: https://cloud.example.com/files/shares/with-me
---
OpenCloud - a safe home for all your data
https://opencloud.eu
`,
expectedSender: sharer.GetDisplayName(),
expectedLink: testOpenCloudURL + "/files/shares/with-me",
done: make(chan struct{}),
}, events.Event{
Event: events.ShareCreated{
Sharer: sharer.GetId(),
GranteeUserID: sharee.GetId(),
CTime: utils.TimeToTS(time.Date(2023, 4, 17, 16, 42, 0, 0, time.UTC)),
ItemID: resourceid,
},
}),
Entry("Share Expired", testChannel{
expectedReceipients: []string{sharee.GetMail()},
expectedSubject: "Share to 'secrets of the board' expired at 2023-04-17 16:42:00",
expectedTextBody: `Hello Eric Expireling,
Your share to secrets of the board has expired at 2023-04-17 16:42:00
Even though this share has been revoked you still might have access through other shares and/or space memberships.
---
OpenCloud - a safe home for all your data
https://opencloud.eu
`,
expectedSender: sharer.GetDisplayName(),
done: make(chan struct{}),
}, events.Event{
Event: events.ShareExpired{
ShareOwner: sharer.GetId(),
GranteeUserID: sharee.GetId(),
ExpiredAt: time.Date(2023, 4, 17, 16, 42, 0, 0, time.UTC),
ItemID: resourceid,
},
}),
Entry("Share Removed", testChannel{
expectedReceipients: []string{sharee.GetMail()},
expectedSubject: "Dr. S. Harer unshared 'secrets of the board' with you",
expectedTextBody: `Hello Eric Expireling,
Dr. S. Harer has unshared 'secrets of the board' with you.
Even though this share has been revoked you still might have access through other shares and/or space memberships.
---
OpenCloud - a safe home for all your data
https://opencloud.eu
`,
expectedSender: sharer.GetDisplayName(),
done: make(chan struct{}),
}, events.Event{
Event: events.ShareRemoved{
Executant: sharer.GetId(),
GranteeUserID: sharee.GetId(),
ItemID: resourceid,
},
}),
Entry("Added to Space", testChannel{
expectedReceipients: []string{sharee.GetMail()},
expectedSubject: "Dr. S. Harer invited you to join secret space",
expectedTextBody: `Hello Eric Expireling,
Dr. S. Harer has invited you to join "secret space".
Click here to view it: https://cloud.example.com/f/spaceid
---
OpenCloud - a safe home for all your data
https://opencloud.eu
`,
expectedSender: sharer.GetDisplayName(),
expectedLink: testOpenCloudURL + "/f/spaceid",
done: make(chan struct{}),
}, events.Event{
Event: events.SpaceShared{
Executant: sharer.GetId(),
Creator: sharer.GetId(),
GranteeUserID: sharee.GetId(),
ID: &provider.StorageSpaceId{OpaqueId: "spaceid"},
},
}),
Entry("Removed from Space", testChannel{
expectedReceipients: []string{sharee.GetMail()},
expectedSubject: "Dr. S. Harer removed you from secret space",
expectedTextBody: `Hello Eric Expireling,
Dr. S. Harer has removed you from "secret space".
You might still have access through your other groups or direct membership.
Click here to check it: https://cloud.example.com/f/spaceid
---
OpenCloud - a safe home for all your data
https://opencloud.eu
`,
expectedSender: sharer.GetDisplayName(),
expectedLink: testOpenCloudURL + "/f/spaceid",
done: make(chan struct{}),
}, events.Event{
Event: events.SpaceUnshared{
Executant: sharer.GetId(),
GranteeUserID: sharee.GetId(),
ID: &provider.StorageSpaceId{OpaqueId: "spaceid"},
},
}),
Entry("Space Expired", testChannel{
expectedReceipients: []string{sharee.GetMail()},
expectedSubject: "Membership of 'secret space' expired at 2023-04-17 16:42:00",
expectedTextBody: `Hello Eric Expireling,
Your membership of space secret space has expired at 2023-04-17 16:42:00
Even though this membership has expired you still might have access through other shares and/or space memberships
---
OpenCloud - a safe home for all your data
https://opencloud.eu
`,
expectedSender: "",
done: make(chan struct{}),
}, events.Event{
Event: events.SpaceMembershipExpired{
SpaceOwner: sharer.GetId(),
GranteeUserID: sharee.GetId(),
SpaceID: &provider.StorageSpaceId{OpaqueId: "spaceid"},
SpaceName: "secret space",
ExpiredAt: time.Date(2023, 4, 17, 16, 42, 0, 0, time.UTC),
},
}),
Entry("Guest Token Created", testChannel{
expectedReceipients: []string{"guest@example.com"},
expectedSubject: "Dr. S. Harer shared 'guest folder' with you",
expectedTextBody: `Hello,
Dr. S. Harer has shared "guest folder" with you.
The link below is personal and can only be used once. Please do not forward it.
The link is only valid for 30 minutes.
Click here to view it: https://cloud.example.com/g/v1.sharehash.secret
---
OpenCloud - a safe home for all your data
https://opencloud.eu
`,
expectedHTMLParts: []string{
`<p style="margin: 0;">Hello,</p>`,
`<p style="margin: 12px 0 0;">Dr. S. Harer has shared &#34;guest folder&#34; with you.</p>`,
`<p style="margin: 12px 0 0;">The link below is personal and can only be used once. Please do not forward it.</p>`,
`>Open link</a>`,
`The link is only valid for 30 minutes.</td>`,
},
expectedSender: sharer.GetDisplayName(),
expectedLink: testOpenCloudURL + "/g/v1.sharehash.secret",
done: make(chan struct{}),
}, events.Event{
Event: ocEvents.GuestTokenCreated{
Sharer: sharer.GetId(),
GranteeEmail: "guest@example.com",
ItemID: resourceid,
ResourceName: "guest folder",
Token: "v1.sharehash.secret",
},
}),
Entry("Guest Token Renewed", testChannel{
expectedReceipients: []string{"guest@example.com"},
expectedSubject: "Your new link to 'secrets of the board'",
expectedTextBody: `Hello,
A new link to "secrets of the board", shared with you by Dr. S. Harer, has been requested.
Your PIN: 123456
Open the link below or enter the PIN on the page where you requested the new link. The link and the PIN are personal and can only be used once. Please do not forward them.
The link and the PIN are only valid for 30 minutes.
Click here to view it: https://cloud.example.com/g/v1.sharehash.newsecret
---
OpenCloud - a safe home for all your data
https://opencloud.eu
`,
expectedSender: sharer.GetDisplayName(),
expectedLink: testOpenCloudURL + "/g/v1.sharehash.newsecret",
done: make(chan struct{}),
}, events.Event{
Event: ocEvents.GuestTokenRenewed{
Sharer: sharer.GetId(),
GranteeEmail: "guest@example.com",
ItemID: resourceid,
Token: "v1.sharehash.newsecret",
Pin: "123456",
},
}),
Entry("Guest Token Renewed without PIN", testChannel{
expectedReceipients: []string{"guest@example.com"},
expectedSubject: "Your new link to 'secrets of the board'",
expectedTextBody: `Hello,
A new link to "secrets of the board", shared with you by Dr. S. Harer, has been requested.
The link below is personal and can only be used once. Please do not forward it.
The link is only valid for 30 minutes.
Click here to view it: https://cloud.example.com/g/v1.sharehash.newsecret
---
OpenCloud - a safe home for all your data
https://opencloud.eu
`,
expectedSender: sharer.GetDisplayName(),
expectedLink: testOpenCloudURL + "/g/v1.sharehash.newsecret",
done: make(chan struct{}),
}, events.Event{
Event: ocEvents.GuestTokenRenewed{
Sharer: sharer.GetId(),
GranteeEmail: "guest@example.com",
ItemID: resourceid,
Token: "v1.sharehash.newsecret",
},
}),
)
})
var _ = Describe("Notifications X-Site Scripting", func() {
var (
gatewayClient *cs3mocks.GatewayAPIClient
gatewaySelector pool.Selectable[gateway.GatewayAPIClient]
vs *settingsmocks.ValueService
sharer = &user.User{
Id: &user.UserId{
OpaqueId: "sharer",
},
Mail: "sharer@opencloud.eu",
DisplayName: "Dr. O'reilly",
}
sharee = &user.User{
Id: &user.UserId{
OpaqueId: "sharee",
},
Mail: "sharee@opencloud.eu",
DisplayName: "<script>alert('Eric Expireling');</script>",
}
resourceid = &provider.ResourceId{
StorageId: "storageid",
SpaceId: "spaceid",
OpaqueId: "itemid",
}
)
BeforeEach(func() {
pool.RemoveSelector("GatewaySelector" + "eu.opencloud.api.gateway")
gatewayClient = &cs3mocks.GatewayAPIClient{}
gatewaySelector = pool.GetSelector[gateway.GatewayAPIClient](
"GatewaySelector",
"eu.opencloud.api.gateway",
func(cc grpc.ClientConnInterface) gateway.GatewayAPIClient {
return gatewayClient
},
)
gatewayClient.On("GetUser", mock.Anything, mock.MatchedBy(func(req *user.GetUserRequest) bool {
return req.GetUserId().GetOpaqueId() == sharer.GetId().GetOpaqueId()
})).Return(&user.GetUserResponse{Status: &rpc.Status{Code: rpc.Code_CODE_OK}, User: sharer}, nil)
gatewayClient.On("GetUser", mock.Anything, mock.MatchedBy(func(req *user.GetUserRequest) bool {
return req.GetUserId().GetOpaqueId() == sharee.GetId().GetOpaqueId()
})).Return(&user.GetUserResponse{Status: &rpc.Status{Code: rpc.Code_CODE_OK}, User: sharee}, nil)
gatewayClient.On("Authenticate", mock.Anything, mock.Anything).Return(&gateway.AuthenticateResponse{Status: &rpc.Status{Code: rpc.Code_CODE_OK}, User: sharer}, nil)
gatewayClient.On("Stat", mock.Anything, mock.Anything).Return(&provider.StatResponse{
Status: &rpc.Status{Code: rpc.Code_CODE_OK},
Info: &provider.ResourceInfo{
Name: "<script>alert('secrets of the board');</script>",
Space: &provider.StorageSpace{Name: "<script>alert('secret space');</script>"}},
}, nil)
vs = &settingsmocks.ValueService{}
vs.On("GetValueByUniqueIdentifiers", mock.Anything, mock.Anything).Return(&settingssvc.GetValueResponse{
Value: &settingsmsg.ValueWithIdentifier{
Value: &settingsmsg.Value{
Value: &settingsmsg.Value_CollectionValue{
CollectionValue: &settingsmsg.CollectionValue{
Values: []*settingsmsg.CollectionOption{
{
Key: "mail",
Option: &settingsmsg.CollectionOption_BoolValue{BoolValue: true},
},
},
},
},
},
},
}, nil)
})
DescribeTable("Sending userEventIds",
func(tc testChannel, ev events.Event) {
cfg := defaults.FullDefaultConfig()
cfg.GRPCClientTLS = &shared.GRPCClientTLS{}
ch := make(chan events.Event)
evts := service.NewEventsNotifier(ch, tc, log.NewLogger(), gatewaySelector, vs, "",
"", "", "", testOpenCloudURL, "", "",
store.Create(), nil, nil)
go evts.Run()
ch <- ev
select {
case <-tc.done:
// finished
case <-time.Tick(3 * time.Second):
Fail("timeout waiting for notification")
}
},
Entry("Share Created", testChannel{
expectedReceipients: []string{sharee.GetMail()},
expectedSubject: "Dr. O'reilly shared '<script>alert('secrets of the board');</script>' with you",
expectedTextBody: `Hello <script>alert('Eric Expireling');</script>
Dr. O'reilly has shared "<script>alert('secrets of the board');</script>" with you.
Click here to view it: https://cloud.example.com/files/shares/with-me
---
OpenCloud - a safe home for all your data
https://opencloud.eu
`,
expectedHTMLParts: []string{
`<p style="margin: 0;">Hello &lt;script&gt;alert(&#39;Eric Expireling&#39;);&lt;/script&gt;</p>`,
`<p style="margin: 12px 0 0;">Dr. O&#39;reilly has shared &#34;&lt;script&gt;alert(&#39;secrets of the board&#39;);&lt;/script&gt;&#34; with you.</p>`,
`>View shares</a>`,
},
expectedSender: sharer.GetDisplayName(),
expectedLink: testOpenCloudURL + "/files/shares/with-me",
done: make(chan struct{}),
}, events.Event{
Event: events.ShareCreated{
Sharer: sharer.GetId(),
GranteeUserID: sharee.GetId(),
CTime: utils.TimeToTS(time.Date(2023, 4, 17, 16, 42, 0, 0, time.UTC)),
ItemID: resourceid,
},
}),
Entry("Added to Space", testChannel{
expectedReceipients: []string{sharee.GetMail()},
expectedSubject: "Dr. O'reilly invited you to join <script>alert('secret space');</script>",
expectedTextBody: `Hello <script>alert('Eric Expireling');</script>,
Dr. O'reilly has invited you to join "<script>alert('secret space');</script>".
Click here to view it: https://cloud.example.com/f/spaceid
---
OpenCloud - a safe home for all your data
https://opencloud.eu
`,
expectedSender: sharer.GetDisplayName(),
expectedHTMLParts: []string{
`<p style="margin: 0;">Hello &lt;script&gt;alert(&#39;Eric Expireling&#39;);&lt;/script&gt;,</p>`,
`<p style="margin: 12px 0 0;">Dr. O&#39;reilly has invited you to join &#34;&lt;script&gt;alert(&#39;secret space&#39;);&lt;/script&gt;&#34;.</p>`,
`>Open space</a>`,
},
expectedLink: testOpenCloudURL + "/f/spaceid",
done: make(chan struct{}),
}, events.Event{
Event: events.SpaceShared{
Executant: sharer.GetId(),
Creator: sharer.GetId(),
GranteeUserID: sharee.GetId(),
ID: &provider.StorageSpaceId{OpaqueId: "spaceid"},
},
}),
)
})
const testOpenCloudURL = "https://cloud.example.com"
type testChannel struct {
expectedReceipients []string
expectedSubject string
expectedTextBody string
expectedHTMLParts []string
expectedSender string
// expectedLink, if set, must be an absolute URL on the OpenCloud instance
// and must be linked in the HTML body.
expectedLink string
done chan struct{}
}
func (tc testChannel) SendMessage(ctx context.Context, m *channels.Message) error {
defer GinkgoRecover()
Expect(tc.expectedReceipients).To(Equal(m.Recipient))
Expect(tc.expectedSubject).To(Equal(m.Subject))
Expect(tc.expectedTextBody).To(Equal(m.TextBody))
Expect(tc.expectedSender).To(Equal(m.Sender))
Expect(m.HTMLBody).ToNot(BeEmpty())
for _, part := range tc.expectedHTMLParts {
Expect(m.HTMLBody).To(ContainSubstring(part))
}
// user input must never reach the html body unescaped
Expect(m.HTMLBody).ToNot(ContainSubstring("<script>"))
if tc.expectedLink != "" {
Expect(tc.expectedLink).To(HavePrefix(testOpenCloudURL + "/"))
Expect(m.TextBody).To(ContainSubstring(tc.expectedLink))
Expect(m.HTMLBody).To(ContainSubstring(`href="` + tc.expectedLink + `"`))
}
tc.done <- struct{}{}
return nil
}