-
Notifications
You must be signed in to change notification settings - Fork 310
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
is: Add test for sending emails if admin
- Loading branch information
Showing
4 changed files
with
74 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,12 +17,14 @@ package identityserver | |
import ( | ||
"os" | ||
"testing" | ||
"time" | ||
|
||
"go.thethings.network/lorawan-stack/v3/pkg/identityserver/storetest" | ||
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb" | ||
"go.thethings.network/lorawan-stack/v3/pkg/util/test" | ||
"go.thethings.network/lorawan-stack/v3/pkg/util/test/assertions/should" | ||
"google.golang.org/grpc" | ||
"google.golang.org/protobuf/types/known/timestamppb" | ||
) | ||
|
||
func TestEmailNotificationPreferences(t *testing.T) { | ||
|
@@ -31,36 +33,40 @@ func TestEmailNotificationPreferences(t *testing.T) { | |
admin := p.NewUser() | ||
admin.Admin = true | ||
adminKey, _ := p.NewAPIKey(admin.GetEntityIdentifiers(), ttnpb.Right_RIGHT_ALL) | ||
adminCreds := rpcCreds(adminKey) | ||
admin.EmailNotificationPreferences = &ttnpb.EmailNotificationPreferences{ | ||
Types: []ttnpb.NotificationType{ | ||
ttnpb.NotificationType_API_KEY_CHANGED, | ||
ttnpb.NotificationType_API_KEY_CREATED, | ||
}, | ||
} | ||
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) | ||
usr1Key, _ := p.NewAPIKey(usr1.GetEntityIdentifiers(), | ||
ttnpb.Right_RIGHT_APPLICATION_INFO, | ||
ttnpb.Right_RIGHT_APPLICATION_LINK, | ||
ttnpb.Right_RIGHT_APPLICATION_SETTINGS_API_KEYS, | ||
) | ||
usr1Creds := rpcCreds(usr1Key) | ||
|
||
app1 := p.NewApplication(usr1.GetOrganizationOrUserIdentifiers()) | ||
limitedKey, _ := p.NewAPIKey(usr1.GetEntityIdentifiers(), | ||
ttnpb.Right_RIGHT_APPLICATION_INFO, | ||
app1 := p.NewApplication(admin.GetOrganizationOrUserIdentifiers()) | ||
p.NewMembership( | ||
usr1.GetOrganizationOrUserIdentifiers(), | ||
app1.GetEntityIdentifiers(), | ||
ttnpb.Right_RIGHT_APPLICATION_SETTINGS_BASIC, | ||
ttnpb.Right_RIGHT_APPLICATION_SETTINGS_API_KEYS, | ||
ttnpb.Right_RIGHT_APPLICATION_LINK, | ||
) | ||
limitedCreds := rpcCreds(limitedKey) | ||
|
||
appKey, _ := p.NewAPIKey(app1.GetEntityIdentifiers(), | ||
ttnpb.Right_RIGHT_APPLICATION_INFO, | ||
ttnpb.Right_RIGHT_APPLICATION_LINK, | ||
ttnpb.Right_RIGHT_APPLICATION_SETTINGS_API_KEYS, | ||
) | ||
|
||
now := timestamppb.Now() | ||
usrIDs := &ttnpb.UserIdentifiers{ | ||
UserId: "foo-usr", | ||
} | ||
|
||
t.Parallel() | ||
a, ctx := test.New(t) | ||
|
||
|
@@ -71,31 +77,59 @@ func TestEmailNotificationPreferences(t *testing.T) { | |
is.config.Email.Dir = tempDir | ||
|
||
reg := ttnpb.NewApplicationAccessClient(cc) | ||
userReg := ttnpb.NewUserRegistryClient(cc) | ||
|
||
// Test sending email to users that have API_KEY_CHANGED in their preferences. | ||
// Test user not receiving email notification because this | ||
// notification type is not in the list of 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_SETTINGS_API_KEYS, | ||
ttnpb.Right_RIGHT_APPLICATION_LINK, | ||
}, | ||
}, | ||
FieldMask: ttnpb.FieldMask("rights"), | ||
}, limitedCreds) | ||
}, adminCreds) | ||
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_SETTINGS_API_KEYS, | ||
ttnpb.Right_RIGHT_APPLICATION_LINK, | ||
}) | ||
} | ||
|
||
entries, err := os.ReadDir(tempDir) | ||
a.So(err, should.BeNil) | ||
a.So(entries, should.HaveLength, 0) | ||
|
||
// Test admin receiving email notification in spite of the list of preferences. | ||
updatedUser, err := userReg.Create(ctx, &ttnpb.CreateUserRequest{ | ||
User: &ttnpb.User{ | ||
Ids: usrIDs, | ||
Password: "test password", | ||
CreatedAt: now, | ||
UpdatedAt: now, | ||
Name: "Foo User", | ||
Description: "Foo User Description", | ||
PrimaryEmailAddress: "[email protected]", | ||
State: ttnpb.State_STATE_REQUESTED, | ||
}, | ||
}, adminCreds) | ||
if a.So(err, should.BeNil) && a.So(updatedUser, should.NotBeNil) { | ||
a.So(updatedUser.State, should.Equal, ttnpb.State_STATE_REQUESTED) | ||
} | ||
|
||
entries, err = os.ReadDir(tempDir) | ||
a.So(err, should.BeNil) | ||
a.So(entries, should.HaveLength, 1) | ||
|
||
for _, opts := range [][]grpc.CallOption{{adminCreds}, {usr1Creds}, {limitedCreds}} { | ||
time.Sleep(test.Delay) | ||
|
||
// Test users receiving email notification because this notification type is in the list of preferences. | ||
for _, opts := range [][]grpc.CallOption{{adminCreds}, {usr1Creds}} { | ||
created, err := reg.CreateAPIKey(ctx, &ttnpb.CreateApplicationAPIKeyRequest{ | ||
ApplicationIds: app1.GetIds(), | ||
Name: "api-key-name", | ||
|
@@ -106,5 +140,9 @@ func TestEmailNotificationPreferences(t *testing.T) { | |
a.So(created.Rights, should.Resemble, []ttnpb.Right{ttnpb.Right_RIGHT_APPLICATION_INFO}) | ||
} | ||
} | ||
|
||
entries, err = os.ReadDir(tempDir) | ||
a.So(err, should.BeNil) | ||
a.So(entries, should.HaveLength, 3) | ||
}, withPrivateTestDatabase(p)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters