Skip to content

Commit

Permalink
is: Add email notification preferences flow test
Browse files Browse the repository at this point in the history
  • Loading branch information
ryaplots committed Oct 24, 2024
1 parent 744a7bf commit 18ad2cb
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
71 changes: 71 additions & 0 deletions pkg/identityserver/email_notification_preferences_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright © 2024 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package identityserver

import (
"fmt"
"os"
"path/filepath"
"testing"

"go.thethings.network/lorawan-stack/v3/pkg/errors"
"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"
)

func TestEmailNotificationPreferences(t *testing.T) {
p := &storetest.Population{}
admin := p.NewUser()
admin.Admin = true
admin.EmailNotificationPreferences = &ttnpb.EmailNotificationPreferences{
Types: []ttnpb.NotificationType{
ttnpb.NotificationType_API_KEY_CREATED,
},
}
adminKey, _ := p.NewAPIKey(admin.GetEntityIdentifiers(), ttnpb.Right_RIGHT_ALL)
adminCreds := rpcCreds(adminKey)

t.Parallel()
a, ctx := test.New(t)

testWithIdentityServer(t, func(is *IdentityServer, cc *grpc.ClientConn) {
is.config.Email.Provider = "dir"
tempDir := t.TempDir()
is.config.Email.Dir = tempDir

reg := ttnpb.NewUserAccessClient(cc)

apiKey, err := reg.CreateAPIKey(ctx, &ttnpb.CreateUserAPIKeyRequest{
UserIds: admin.GetIds(),
Name: "api-key-name",
Rights: []ttnpb.Right{ttnpb.Right_RIGHT_USER_ALL},
}, adminCreds)
if a.So(err, should.NotBeNil) {
a.So(errors.IsNotFound(err), should.BeTrue)
}
a.So(apiKey, should.BeNil)

entries, err := os.ReadDir(tempDir)
if a.So(err, should.BeNil) && a.So(entries, should.HaveLength, 1) {
data, err := os.ReadFile(filepath.Join(tempDir, entries[0].Name()))
fmt.Printf("/////////////data: %v\n", string(data))
a.So(err, should.BeNil)
a.So(string(data), should.ContainSubstring, "<p>A new API key has just been created for your user")
}
})
}
5 changes: 5 additions & 0 deletions pkg/identityserver/storetest/user_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,11 @@ func (st *StoreTest) TestUserStoreCRUD(t *T) {
},
},
)
a.So(updated.EmailNotificationPreferences, should.Resemble, &ttnpb.EmailNotificationPreferences{
Types: []ttnpb.NotificationType{
ttnpb.NotificationType_API_KEY_CREATED,
},
})
a.So(*ttnpb.StdTime(updated.CreatedAt), should.Equal, *ttnpb.StdTime(created.CreatedAt))
a.So(*ttnpb.StdTime(updated.UpdatedAt), should.HappenWithin, 5*time.Second, start)
}
Expand Down

0 comments on commit 18ad2cb

Please sign in to comment.