Skip to content

Commit

Permalink
MG-888 - Update users SDK tests (#2329)
Browse files Browse the repository at this point in the history
Signed-off-by: 1998-felix <[email protected]>
  • Loading branch information
felixgateru authored Jul 4, 2024
1 parent c518c0b commit 8596b73
Show file tree
Hide file tree
Showing 7 changed files with 1,935 additions and 922 deletions.
3 changes: 1 addition & 2 deletions pkg/sdk/go/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ func TestHealth(t *testing.T) {
auth.Test(t)
defer ths.Close()

usclsv, _, _, auth := setupUsers()
auth.Test(t)
usclsv, _ := setupUsers()
defer usclsv.Close()

CertTs, _ := setupCerts()
Expand Down
11 changes: 11 additions & 0 deletions pkg/sdk/go/sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,17 @@ type SDK interface {
// fmt.Println(users)
Users(pm PageMetadata, token string) (UsersPage, errors.SDKError)

// Members returns list of users that are members of a group.
//
// example:
// pm := sdk.PageMetadata{
// Offset: 0,
// Limit: 10,
// }
// members, _ := sdk.Members("groupID", pm, "token")
// fmt.Println(members)
Members(groupID string, meta PageMetadata, token string) (UsersPage, errors.SDKError)

// UserProfile returns user logged in.
//
// example:
Expand Down
37 changes: 26 additions & 11 deletions pkg/sdk/go/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import (
"os"
"regexp"
"testing"
"time"

"github.com/absmach/magistrala/invitations"
mgclients "github.com/absmach/magistrala/pkg/clients"
mggroups "github.com/absmach/magistrala/pkg/groups"
sdk "github.com/absmach/magistrala/pkg/sdk/go"
"github.com/absmach/magistrala/pkg/uuid"
"github.com/absmach/magistrala/users/hasher"
umocks "github.com/absmach/magistrala/users/mocks"
"github.com/stretchr/testify/assert"
)

Expand All @@ -32,14 +32,8 @@ var (
idProvider = uuid.New()
phasher = hasher.New()
validMetadata = sdk.Metadata{"role": "client"}
user = sdk.User{
Name: "clientname",
Tags: []string{"tag1", "tag2"},
Credentials: sdk.Credentials{Identity: "clientidentity", Secret: secret},
Metadata: validMetadata,
Status: mgclients.EnabledStatus.String(),
}
thing = sdk.Thing{
user = generateTestUser(&testing.T{})
thing = sdk.Thing{
Name: "thingname",
Tags: []string{"tag1", "tag2"},
Credentials: sdk.Credentials{Identity: "clientidentity", Secret: generateUUID(&testing.T{})},
Expand All @@ -55,7 +49,6 @@ var (

subject = generateUUID(&testing.T{})
object = generateUUID(&testing.T{})
emailer = new(umocks.Emailer)
passRegex = regexp.MustCompile("^.{8,}$")
)

Expand Down Expand Up @@ -180,7 +173,10 @@ func convertClient(c sdk.User) mgclients.Client {
if err != nil {
return mgclients.Client{}
}

role, err := mgclients.ToRole(c.Role)
if err != nil {
return mgclients.Client{}
}
return mgclients.Client{
ID: c.ID,
Name: c.Name,
Expand All @@ -191,6 +187,7 @@ func convertClient(c sdk.User) mgclients.Client {
CreatedAt: c.CreatedAt,
UpdatedAt: c.UpdatedAt,
Status: status,
Role: role,
}
}

Expand Down Expand Up @@ -252,6 +249,24 @@ func convertInvitation(i sdk.Invitation) invitations.Invitation {
}
}

func generateTestUser(t *testing.T) sdk.User {
createdAt, err := time.Parse(time.RFC3339, "2024-01-01T00:00:00Z")
assert.Nil(t, err, fmt.Sprintf("Unexpected error parsing time: %v", err))
return sdk.User{
ID: generateUUID(t),
Name: "clientname",
Credentials: sdk.Credentials{
Identity: "[email protected]",
Secret: secret,
},
Tags: []string{"tag1", "tag2"},
Metadata: validMetadata,
CreatedAt: createdAt,
UpdatedAt: createdAt,
Status: mgclients.EnabledStatus.String(),
}
}

func TestMain(m *testing.M) {
exitCode := m.Run()
os.Exit(exitCode)
Expand Down
Loading

0 comments on commit 8596b73

Please sign in to comment.