NOISSUE - Fix e2e Testing Tool (#135)

* fix(e2e): Add admin role and permission

This commit adds a new constant and modifies the `createUser` function. The function now creates a user with an admin role and a domain with admin permissions.

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>

* chore(ci): add e2e testing on CI

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>

* Remove unnecessary time.Sleep calls and optimize code execution

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>

---------

Signed-off-by: Rodney Osodo <28790446+rodneyosodo@users.noreply.github.com>
This commit is contained in:
b1ackd0t
2023-12-15 13:20:48 +03:00
committed by GitHub
parent 450a8f94a2
commit 6d2096cda9
+53 -31
View File
@@ -27,6 +27,7 @@ const (
batchSize = 99
usersPort = "9002"
thingsPort = "9000"
domainsPort = "8189"
)
var (
@@ -66,6 +67,7 @@ func Test(conf Config) {
sdkConf := sdk.Config{
ThingsURL: fmt.Sprintf("http://%s:%s", conf.Host, thingsPort),
UsersURL: fmt.Sprintf("http://%s:%s", conf.Host, usersPort),
DomainsURL: fmt.Sprintf("http://%s:%s", conf.Host, domainsPort),
HTTPAdapterURL: fmt.Sprintf("http://%s/http", conf.Host),
MsgContentType: sdk.CTJSONSenML,
TLSVerification: false,
@@ -105,15 +107,12 @@ func Test(conf Config) {
}
color.Success.Printf("created channels of ids:\n%s\n", magenta(getIDS(channels)))
time.Sleep(5 * time.Second)
// List users, groups, things and channels
if err := read(s, conf, token, users, groups, things, channels); err != nil {
errExit(fmt.Errorf("unable to read users, groups, things and channels: %w", err))
}
color.Success.Println("viewed users, groups, things and channels")
time.Sleep(5 * time.Second)
// Update users, groups, things and channels
if err := update(s, token, users, groups, things, channels); err != nil {
errExit(fmt.Errorf("unable to update users, groups, things and channels: %w", err))
@@ -134,19 +133,46 @@ func errExit(err error) {
func createUser(s sdk.SDK, conf Config) (string, error) {
user := sdk.User{
Name: fmt.Sprintf("%s-%s", conf.Prefix, namesgenerator.Generate()),
Name: fmt.Sprintf("%s%s", conf.Prefix, namesgenerator.Generate()),
Credentials: sdk.Credentials{
Identity: fmt.Sprintf("%s-%s@email.com", conf.Prefix, namesgenerator.Generate()),
Identity: fmt.Sprintf("%s%s@email.com", conf.Prefix, namesgenerator.Generate()),
Secret: defPass,
},
Status: sdk.EnabledStatus,
Role: "admin",
}
if _, err := s.CreateUser(user, ""); err != nil {
return "", fmt.Errorf("unable to create user: %w", err)
}
token, err := s.CreateToken(sdk.Login{Identity: user.Credentials.Identity, Secret: user.Credentials.Secret})
login := sdk.Login{
Identity: user.Credentials.Identity,
Secret: user.Credentials.Secret,
}
token, err := s.CreateToken(login)
if err != nil {
return "", fmt.Errorf("unable to login user: %w", err)
}
dname := fmt.Sprintf("%s%s", conf.Prefix, namesgenerator.Generate())
domain := sdk.Domain{
Name: dname,
Alias: strings.ToLower(dname),
Permission: "admin",
}
domain, err = s.CreateDomain(domain, token.AccessToken)
if err != nil {
return "", fmt.Errorf("unable to create domain: %w", err)
}
login = sdk.Login{
Identity: user.Credentials.Identity,
Secret: user.Credentials.Secret,
DomainID: domain.ID,
}
token, err = s.CreateToken(login)
if err != nil {
return "", fmt.Errorf("unable to login user: %w", err)
}
@@ -160,9 +186,9 @@ func createUsers(s sdk.SDK, conf Config, token string) ([]sdk.User, error) {
for i := uint64(0); i < conf.Num; i++ {
user := sdk.User{
Name: fmt.Sprintf("%s-%s", conf.Prefix, namesgenerator.Generate()),
Name: fmt.Sprintf("%s%s", conf.Prefix, namesgenerator.Generate()),
Credentials: sdk.Credentials{
Identity: fmt.Sprintf("%s-%s@email.com", conf.Prefix, namesgenerator.Generate()),
Identity: fmt.Sprintf("%s%s@email.com", conf.Prefix, namesgenerator.Generate()),
Secret: defPass,
},
Status: sdk.EnabledStatus,
@@ -184,7 +210,7 @@ func createGroups(s sdk.SDK, conf Config, token string) ([]sdk.Group, error) {
for i := uint64(0); i < conf.Num; i++ {
group := sdk.Group{
Name: fmt.Sprintf("%s-%s", conf.Prefix, namesgenerator.Generate()),
Name: fmt.Sprintf("%s%s", conf.Prefix, namesgenerator.Generate()),
Status: sdk.EnabledStatus,
}
@@ -204,7 +230,7 @@ func createThingsInBatch(s sdk.SDK, conf Config, token string, num uint64) ([]sd
for i := uint64(0); i < num; i++ {
things[i] = sdk.Thing{
Name: fmt.Sprintf("%s-%s", conf.Prefix, namesgenerator.Generate()),
Name: fmt.Sprintf("%s%s", conf.Prefix, namesgenerator.Generate()),
}
}
@@ -250,7 +276,7 @@ func createChannelsInBatch(s sdk.SDK, conf Config, token string, num uint64) ([]
for i := uint64(0); i < num; i++ {
channels[i] = sdk.Channel{
Name: fmt.Sprintf("%s-%s", conf.Prefix, namesgenerator.Generate()),
Name: fmt.Sprintf("%s%s", conf.Prefix, namesgenerator.Generate()),
}
channels[i], err = s.CreateChannel(channels[i], token)
if err != nil {
@@ -292,51 +318,51 @@ func createChannels(s sdk.SDK, conf Config, token string) ([]sdk.Channel, error)
func read(s sdk.SDK, conf Config, token string, users []sdk.User, groups []sdk.Group, things []sdk.Thing, channels []sdk.Channel) error {
for _, user := range users {
if _, err := s.User(user.ID, token); err != nil {
return err
return fmt.Errorf("failed to get user %w", err)
}
}
up, err := s.Users(sdk.PageMetadata{}, token)
if err != nil {
return err
return fmt.Errorf("failed to get users %w", err)
}
if up.Total != conf.Num {
return fmt.Errorf("returned users %d not equal to create users %d", up.Total, conf.Num)
if up.Total < conf.Num {
return fmt.Errorf("returned users %d less than created users %d", up.Total, conf.Num)
}
for _, group := range groups {
if _, err := s.Group(group.ID, token); err != nil {
return err
return fmt.Errorf("failed to get group %w", err)
}
}
gp, err := s.Groups(sdk.PageMetadata{}, token)
if err != nil {
return err
return fmt.Errorf("failed to get groups %w", err)
}
if gp.Total != conf.Num {
return fmt.Errorf("returned groups %d not equal to create groups %d", gp.Total, conf.Num)
if gp.Total < conf.Num {
return fmt.Errorf("returned groups %d less than created groups %d", gp.Total, conf.Num)
}
for _, thing := range things {
if _, err := s.Thing(thing.ID, token); err != nil {
return err
return fmt.Errorf("failed to get thing %w", err)
}
}
tp, err := s.Things(sdk.PageMetadata{}, token)
if err != nil {
return err
return fmt.Errorf("failed to get things %w", err)
}
if tp.Total != conf.Num {
return fmt.Errorf("returned things %d not equal to create things %d", tp.Total, conf.Num)
if tp.Total < conf.Num {
return fmt.Errorf("returned things %d less than created things %d", tp.Total, conf.Num)
}
for _, channel := range channels {
if _, err := s.Channel(channel.ID, token); err != nil {
return err
return fmt.Errorf("failed to get channel %w", err)
}
}
cp, err := s.Channels(sdk.PageMetadata{}, token)
if err != nil {
return err
return fmt.Errorf("failed to get channels %w", err)
}
if cp.Total != conf.Num {
return fmt.Errorf("returned channels %d not equal to create channels %d", cp.Total, conf.Num)
if cp.Total < conf.Num {
return fmt.Errorf("returned channels %d less than created channels %d", cp.Total, conf.Num)
}
return nil
@@ -500,8 +526,6 @@ func update(s sdk.SDK, token string, users []sdk.User, groups []sdk.Group, thing
}
func messaging(s sdk.SDK, conf Config, token string, things []sdk.Thing, channels []sdk.Channel) error {
time.Sleep(5 * time.Second)
for _, thing := range things {
for _, channel := range channels {
conn := sdk.Connection{
@@ -514,8 +538,6 @@ func messaging(s sdk.SDK, conf Config, token string, things []sdk.Thing, channel
}
}
time.Sleep(5 * time.Second)
g := new(errgroup.Group)
bt := time.Now().Unix()