test(GODT-2871): tests for new telemetry logic.

This commit is contained in:
Jakub
2023-08-22 08:35:46 +02:00
parent f557666b4d
commit a731237701
12 changed files with 221 additions and 206 deletions

View File

@ -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

View File

@ -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

View File

@ -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
}

View File

@ -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}))

View File

@ -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)
}

View File

@ -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()