Skip to content

Commit

Permalink
is: Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaplots committed Oct 25, 2024
1 parent b33ef4f commit e4b4947
Showing 1 changed file with 54 additions and 7 deletions.
61 changes: 54 additions & 7 deletions pkg/identityserver/email_notification_preferences_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package identityserver

import (
"os"
"testing"

"go.thethings.network/lorawan-stack/v3/pkg/identityserver/storetest"
Expand All @@ -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",
Expand All @@ -59,5 +106,5 @@ func TestEmailNotificationPreferences(t *testing.T) {
a.So(created.Rights, should.Resemble, []ttnpb.Right{ttnpb.Right_RIGHT_APPLICATION_INFO})
}
}
})
}, withPrivateTestDatabase(p))
}

0 comments on commit e4b4947

Please sign in to comment.