mirror of https://github.com/portainer/portainer
parent
cdfa9b25a8
commit
8fc5a5e8a1
|
@ -63,7 +63,7 @@ func (service *Service) TeamByName(name string) (*portainer.Team, error) {
|
||||||
logrus.WithField("obj", obj).Errorf("Failed to convert to Team object")
|
logrus.WithField("obj", obj).Errorf("Failed to convert to Team object")
|
||||||
return nil, fmt.Errorf("Failed to convert to Team object: %s", obj)
|
return nil, fmt.Errorf("Failed to convert to Team object: %s", obj)
|
||||||
}
|
}
|
||||||
if strings.EqualFold(t.Name, name) {
|
if strings.EqualFold(team.Name, name) {
|
||||||
t = team
|
t = team
|
||||||
return nil, stop
|
return nil, stop
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,53 @@
|
||||||
|
package tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/portainer/portainer/api/dataservices/errors"
|
||||||
|
"github.com/portainer/portainer/api/datastore"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_teamByName(t *testing.T) {
|
||||||
|
t.Run("When store is empty should return ErrObjectNotFound", func(t *testing.T) {
|
||||||
|
_, store, teardown := datastore.MustNewTestStore(true)
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
_, err := store.Team().TeamByName("name")
|
||||||
|
assert.ErrorIs(t, err, errors.ErrObjectNotFound)
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("When there is no object with the same name should return ErrObjectNotFound", func(t *testing.T) {
|
||||||
|
_, store, teardown := datastore.MustNewTestStore(true)
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
teamBuilder := teamBuilder{
|
||||||
|
t: t,
|
||||||
|
store: store,
|
||||||
|
count: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
teamBuilder.createNew("name1")
|
||||||
|
|
||||||
|
_, err := store.Team().TeamByName("name")
|
||||||
|
assert.ErrorIs(t, err, errors.ErrObjectNotFound)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("When there is an object with the same name should return the object", func(t *testing.T) {
|
||||||
|
_, store, teardown := datastore.MustNewTestStore(true)
|
||||||
|
defer teardown()
|
||||||
|
|
||||||
|
teamBuilder := teamBuilder{
|
||||||
|
t: t,
|
||||||
|
store: store,
|
||||||
|
count: 0,
|
||||||
|
}
|
||||||
|
|
||||||
|
expectedTeam := teamBuilder.createNew("name1")
|
||||||
|
|
||||||
|
team, err := store.Team().TeamByName("name1")
|
||||||
|
assert.NoError(t, err, "TeamByName should succeed")
|
||||||
|
assert.Equal(t, expectedTeam, team)
|
||||||
|
})
|
||||||
|
}
|
|
@ -0,0 +1,28 @@
|
||||||
|
package tests
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
portainer "github.com/portainer/portainer/api"
|
||||||
|
"github.com/portainer/portainer/api/datastore"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
type teamBuilder struct {
|
||||||
|
t *testing.T
|
||||||
|
count int
|
||||||
|
store *datastore.Store
|
||||||
|
}
|
||||||
|
|
||||||
|
func (b *teamBuilder) createNew(name string) *portainer.Team {
|
||||||
|
b.count++
|
||||||
|
team := &portainer.Team{
|
||||||
|
ID: portainer.TeamID(b.count),
|
||||||
|
Name: name,
|
||||||
|
}
|
||||||
|
|
||||||
|
err := b.store.Team().Create(team)
|
||||||
|
assert.NoError(b.t, err)
|
||||||
|
|
||||||
|
return team
|
||||||
|
}
|
Loading…
Reference in New Issue