mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-15 14:56:42 +00:00
test(GODT-2871): tests for new telemetry logic.
This commit is contained in:
@ -15,7 +15,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//go:build !build_qa
|
||||
//go:build !build_qa && !test_integration
|
||||
|
||||
package bridge
|
||||
|
||||
|
||||
@ -15,7 +15,7 @@
|
||||
// You should have received a copy of the GNU General Public License
|
||||
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//go:build build_qa
|
||||
//go:build build_qa || test_integration
|
||||
|
||||
package bridge
|
||||
|
||||
|
||||
@ -51,7 +51,7 @@ func NewService(
|
||||
settingsGetter SettingsGetter,
|
||||
eventService userevents.Subscribable,
|
||||
) *Service {
|
||||
s := &Service{
|
||||
return &Service{
|
||||
cpc: cpc.NewCPC(),
|
||||
log: logrus.WithFields(logrus.Fields{
|
||||
"user": userID,
|
||||
@ -66,14 +66,10 @@ func NewService(
|
||||
userID: userID,
|
||||
settingsGetter: settingsGetter,
|
||||
}
|
||||
|
||||
s.initialise()
|
||||
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Service) initialise() {
|
||||
settings, err := s.settingsGetter.GetUserSettings(context.Background())
|
||||
func (s *Service) initialise(ctx context.Context) {
|
||||
settings, err := s.settingsGetter.GetUserSettings(ctx)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Error("Cannot get telemetry settings, asuming off")
|
||||
s.isInitialised = false
|
||||
@ -88,6 +84,8 @@ func (s *Service) initialise() {
|
||||
}
|
||||
|
||||
func (s *Service) Start(ctx context.Context, group *orderedtasks.OrderedCancelGroup) {
|
||||
s.initialise(ctx)
|
||||
|
||||
group.Go(ctx, s.userID, "telemetry-service", s.run)
|
||||
}
|
||||
|
||||
@ -118,7 +116,7 @@ func (s *Service) run(ctx context.Context) {
|
||||
case *isTelemetryEnabledReq:
|
||||
s.log.Debug("Received is telemetry enabled request")
|
||||
if !s.isInitialised {
|
||||
s.initialise()
|
||||
s.initialise(ctx)
|
||||
}
|
||||
|
||||
request.Reply(ctx, s.isTelemetryEnabled, nil)
|
||||
@ -137,8 +135,8 @@ func (s *Service) run(ctx context.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Service) HandleRefreshEvent(_ context.Context, _ proton.RefreshFlag) error {
|
||||
s.initialise()
|
||||
func (s *Service) HandleRefreshEvent(ctx context.Context, _ proton.RefreshFlag) error {
|
||||
s.initialise(ctx)
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@ -78,14 +78,14 @@ func TestService_OnUserSettingsEvent(t *testing.T) {
|
||||
mockSettingsGetter,
|
||||
&userevents.NoOpSubscribable{},
|
||||
)
|
||||
|
||||
require.True(t, service.isInitialised)
|
||||
require.False(t, service.isInitialised)
|
||||
|
||||
ctx := context.Background()
|
||||
group := orderedtasks.NewOrderedCancelGroup(async.NoopPanicHandler{})
|
||||
defer group.CancelAndWait()
|
||||
|
||||
service.Start(ctx, group)
|
||||
require.True(t, service.isInitialised)
|
||||
require.False(t, service.IsTelemetryEnabled(ctx))
|
||||
|
||||
require.NoError(t, service.HandleUserSettingsEvent(context.Background(), &proton.UserSettings{Telemetry: proton.SettingEnabled}))
|
||||
|
||||
@ -46,7 +46,7 @@ func (e EventHandler) OnEvent(ctx context.Context, event proton.Event) error {
|
||||
}
|
||||
|
||||
// Start with user settings because of telemetry.
|
||||
if event.UserSettings != nil {
|
||||
if event.UserSettings != nil && e.UserSettingsHandler != nil {
|
||||
if err := e.UserSettingsHandler.HandleUserSettingsEvent(ctx, event.UserSettings); err != nil {
|
||||
return fmt.Errorf("failed to apply user event: %w", err)
|
||||
}
|
||||
|
||||
@ -85,23 +85,6 @@ func TestUser_AddressMode(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestUser_Telemetry(t *testing.T) {
|
||||
withAPI(t, context.Background(), func(ctx context.Context, s *server.Server, m *proton.Manager) {
|
||||
withAccount(t, s, "username", "password", []string{}, func(string, []string) {
|
||||
withUser(t, ctx, s, m, "username", "password", func(user *User) {
|
||||
// By default, user should have Telemetry enabled.
|
||||
telemetry := user.IsTelemetryEnabled(ctx)
|
||||
require.Equal(t, true, telemetry)
|
||||
|
||||
user.client.Close()
|
||||
// If telemetry cannot be retrieved it is disabled.
|
||||
telemetry = user.IsTelemetryEnabled(ctx)
|
||||
require.Equal(t, false, telemetry)
|
||||
})
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
func withAPI(_ testing.TB, ctx context.Context, fn func(context.Context, *server.Server, *proton.Manager)) { //nolint:revive
|
||||
server := server.New()
|
||||
defer server.Close()
|
||||
|
||||
Reference in New Issue
Block a user