|
| 1 | +// Copyright (c) Abstract Machines |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package events_test |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "testing" |
| 10 | + |
| 11 | + "github.com/absmach/supermq/internal/testsutil" |
| 12 | + "github.com/absmach/supermq/pkg/authn" |
| 13 | + "github.com/absmach/supermq/pkg/errors" |
| 14 | + "github.com/absmach/supermq/users" |
| 15 | + "github.com/absmach/supermq/users/events" |
| 16 | + "github.com/absmach/supermq/users/mocks" |
| 17 | + "github.com/go-chi/chi/v5/middleware" |
| 18 | + "github.com/stretchr/testify/assert" |
| 19 | + "github.com/stretchr/testify/require" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + validSession = authn.Session{ |
| 24 | + UserID: testsutil.GenerateUUID(&testing.T{}), |
| 25 | + } |
| 26 | + validUser = users.User{ |
| 27 | + ID: testsutil.GenerateUUID(&testing.T{}), |
| 28 | + FirstName: "Test", |
| 29 | + LastName: "User", |
| 30 | + |
| 31 | + } |
| 32 | +) |
| 33 | + |
| 34 | +func newEventStoreMiddleware(t *testing.T) (*mocks.Service, users.Service) { |
| 35 | + svc := new(mocks.Service) |
| 36 | + nsvc, err := events.NewEventStoreMiddleware(context.Background(), svc, storeURL) |
| 37 | + require.Nil(t, err, fmt.Sprintf("create events store middleware failed with unexpected error: %s", err)) |
| 38 | + |
| 39 | + return svc, nsvc |
| 40 | +} |
| 41 | + |
| 42 | +func TestRegister(t *testing.T) { |
| 43 | + _, nsvc := newEventStoreMiddleware(t) |
| 44 | + |
| 45 | + validID := testsutil.GenerateUUID(t) |
| 46 | + validCtx := context.WithValue(context.Background(), middleware.RequestIDKey, validID) |
| 47 | + |
| 48 | + cases := []struct { |
| 49 | + desc string |
| 50 | + session authn.Session |
| 51 | + user users.User |
| 52 | + selfRegister bool |
| 53 | + svcRes users.User |
| 54 | + svcErr error |
| 55 | + err error |
| 56 | + }{ |
| 57 | + { |
| 58 | + desc: "register user successfully", |
| 59 | + session: validSession, |
| 60 | + user: validUser, |
| 61 | + selfRegister: true, |
| 62 | + svcRes: validUser, |
| 63 | + svcErr: nil, |
| 64 | + err: nil, |
| 65 | + }, |
| 66 | + } |
| 67 | + for _, tc := range cases { |
| 68 | + t.Run(tc.desc, func(t *testing.T) { |
| 69 | + _, err := nsvc.Register(validCtx, tc.session, tc.user, tc.selfRegister) |
| 70 | + assert.True(t, errors.Contains(err, tc.err), fmt.Sprintf("%s: expected %s got %s\n", tc.desc, tc.err, err)) |
| 71 | + }) |
| 72 | + } |
| 73 | +} |
0 commit comments