From e4b4947a8d169cd1a933033e03f901c9fa9eb239 Mon Sep 17 00:00:00 2001 From: Darya Plotnytska Date: Fri, 25 Oct 2024 14:57:37 +0200 Subject: [PATCH] is: Add test --- .../email_notification_preferences_test.go | 61 ++++++++++++++++--- 1 file changed, 54 insertions(+), 7 deletions(-) diff --git a/pkg/identityserver/email_notification_preferences_test.go b/pkg/identityserver/email_notification_preferences_test.go index 15d172f47e..3779cbc053 100644 --- a/pkg/identityserver/email_notification_preferences_test.go +++ b/pkg/identityserver/email_notification_preferences_test.go @@ -15,6 +15,7 @@ package identityserver import ( + "os" "testing" "go.thethings.network/lorawan-stack/v3/pkg/identityserver/storetest" @@ -30,25 +31,71 @@ func TestEmailNotificationPreferences(t *testing.T) { admin := p.NewUser() admin.Admin = true adminKey, _ := p.NewAPIKey(admin.GetEntityIdentifiers(), ttnpb.Right_RIGHT_ALL) + admin.EmailNotificationPreferences = &ttnpb.EmailNotificationPreferences{ + Types: []ttnpb.NotificationType{ + ttnpb.NotificationType_API_KEY_CHANGED, + }, + } adminCreds := rpcCreds(adminKey) usr1 := p.NewUser() + usr1.EmailNotificationPreferences = &ttnpb.EmailNotificationPreferences{ + Types: []ttnpb.NotificationType{ + ttnpb.NotificationType_API_KEY_CREATED, + ttnpb.NotificationType_API_KEY_CHANGED, + }, + } + usr1Key, _ := p.NewAPIKey(usr1.GetEntityIdentifiers(), ttnpb.Right_RIGHT_ALL) + usr1Creds := rpcCreds(usr1Key) + app1 := p.NewApplication(usr1.GetOrganizationOrUserIdentifiers()) + limitedKey, _ := p.NewAPIKey(usr1.GetEntityIdentifiers(), + ttnpb.Right_RIGHT_APPLICATION_INFO, + ttnpb.Right_RIGHT_APPLICATION_SETTINGS_BASIC, + ttnpb.Right_RIGHT_APPLICATION_SETTINGS_API_KEYS, + ) + limitedCreds := rpcCreds(limitedKey) + + appKey, _ := p.NewAPIKey(app1.GetEntityIdentifiers(), + ttnpb.Right_RIGHT_APPLICATION_INFO, + ttnpb.Right_RIGHT_APPLICATION_LINK, + ) t.Parallel() a, ctx := test.New(t) - // is.config.AdminRights.All = true - // is.config.Email.Provider = "dir" - // tempDir := t.TempDir() - // is.config.Email.Dir = tempDir testWithIdentityServer(t, func(is *IdentityServer, cc *grpc.ClientConn) { is.config.AdminRights.All = true + is.config.Email.Provider = "dir" + tempDir := t.TempDir() + is.config.Email.Dir = tempDir reg := ttnpb.NewApplicationAccessClient(cc) - // API Key CRUD with different valid credentials. - for _, opts := range [][]grpc.CallOption{{adminCreds}} { + // Test sending email to users that have API_KEY_CHANGED in their preferences. + updated, err := reg.UpdateAPIKey(ctx, &ttnpb.UpdateApplicationAPIKeyRequest{ + ApplicationIds: app1.GetIds(), + ApiKey: &ttnpb.APIKey{ + Id: appKey.GetId(), + Rights: []ttnpb.Right{ + ttnpb.Right_RIGHT_APPLICATION_SETTINGS_BASIC, + ttnpb.Right_RIGHT_APPLICATION_LINK, + }, + }, + FieldMask: ttnpb.FieldMask("rights"), + }, limitedCreds) + if a.So(err, should.BeNil) && a.So(updated, should.NotBeNil) { + a.So(updated.Rights, should.Resemble, []ttnpb.Right{ + ttnpb.Right_RIGHT_APPLICATION_SETTINGS_BASIC, + ttnpb.Right_RIGHT_APPLICATION_LINK, + }) + } + + entries, err := os.ReadDir(tempDir) + a.So(err, should.BeNil) + a.So(entries, should.HaveLength, 1) + + for _, opts := range [][]grpc.CallOption{{adminCreds}, {usr1Creds}, {limitedCreds}} { created, err := reg.CreateAPIKey(ctx, &ttnpb.CreateApplicationAPIKeyRequest{ ApplicationIds: app1.GetIds(), Name: "api-key-name", @@ -59,5 +106,5 @@ func TestEmailNotificationPreferences(t *testing.T) { a.So(created.Rights, should.Resemble, []ttnpb.Right{ttnpb.Right_RIGHT_APPLICATION_INFO}) } } - }) + }, withPrivateTestDatabase(p)) }