Skip to content

Commit

Permalink
update some permission default logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hmodi-ns1 committed Jul 23, 2024
1 parent 5744a84 commit e316263
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 218 deletions.
16 changes: 0 additions & 16 deletions rest/account_apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,6 @@ func (s *APIKeysService) Create(a *account.APIKey) (*http.Response, error) {
err error
)

// Replace `manage_jobs` with the new split permissions
if a != nil && a.Permissions.Monitoring.ManageJobs {
a.Permissions.Monitoring.ManageJobs = false
a.Permissions.Monitoring.CreateJobs = true
a.Permissions.Monitoring.UpdateJobs = true
a.Permissions.Monitoring.DeleteJobs = true
}

// If this is DDI then the permissions need to be transformed to DDI-compatible permissions.
if s.client.DDI && a != nil {
ddiAPIKey := apiKeyToDDIAPIKey(a)
Expand Down Expand Up @@ -114,14 +106,6 @@ func (s *APIKeysService) Update(a *account.APIKey) (*http.Response, error) {
err error
)

// Replace `manage_jobs` with the new split permissions
if a != nil && a.Permissions.Monitoring.ManageJobs {
a.Permissions.Monitoring.ManageJobs = false
a.Permissions.Monitoring.CreateJobs = true
a.Permissions.Monitoring.UpdateJobs = true
a.Permissions.Monitoring.DeleteJobs = true
}

// If this is DDI then the permissions need to be transformed to DDI-compatible permissions.
if s.client.DDI && a != nil {
ddiAPIKey := apiKeyToDDIAPIKey(a)
Expand Down
84 changes: 24 additions & 60 deletions rest/account_apikey_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,71 +14,35 @@ import (
)

func TestCreateAPIKey(t *testing.T) {
t.Run("with manage_jobs set to true", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)

var k account.APIKey
require.NoError(t, json.Unmarshal(b, &k))
assert.Nil(t, k.Permissions.Security)
assert.Nil(t, k.Permissions.DHCP)
assert.Nil(t, k.Permissions.IPAM)
assert.False(t, k.Permissions.Monitoring.ManageJobs)
assert.True(t, k.Permissions.Monitoring.CreateJobs)
assert.True(t, k.Permissions.Monitoring.UpdateJobs)
assert.True(t, k.Permissions.Monitoring.DeleteJobs)

_, err = w.Write(b)
require.NoError(t, err)
}))
defer ts.Close()
c := NewClient(nil, SetEndpoint(ts.URL))

k := &account.APIKey{
ID: "id-1",
Key: "key-1",
Name: "name-1",
Permissions: account.PermissionsMap{
Monitoring: account.PermissionsMonitoring{ManageJobs: true},
},
}

_, err := c.APIKeys.Create(k)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
})

t.Run("with manage_jobs not set", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)

var k account.APIKey
require.NoError(t, json.Unmarshal(b, &k))
assert.Nil(t, k.Permissions.Security)
assert.Nil(t, k.Permissions.DHCP)
assert.Nil(t, k.Permissions.IPAM)
assert.False(t, k.Permissions.Monitoring.ManageJobs)
assert.False(t, k.Permissions.Monitoring.CreateJobs)
assert.False(t, k.Permissions.Monitoring.UpdateJobs)
assert.False(t, k.Permissions.Monitoring.DeleteJobs)
var k account.APIKey
require.NoError(t, json.Unmarshal(b, &k))
assert.Nil(t, k.Permissions.Security)
assert.Nil(t, k.Permissions.DHCP)
assert.Nil(t, k.Permissions.IPAM)
assert.False(t, k.Permissions.Monitoring.ManageJobs)
assert.False(t, k.Permissions.Monitoring.CreateJobs)
assert.False(t, k.Permissions.Monitoring.UpdateJobs)
assert.False(t, k.Permissions.Monitoring.DeleteJobs)

_, err = w.Write(b)
require.NoError(t, err)
}))
defer ts.Close()
c := NewClient(nil, SetEndpoint(ts.URL))
_, err = w.Write(b)
require.NoError(t, err)
}))
defer ts.Close()
c := NewClient(nil, SetEndpoint(ts.URL))

k := &account.APIKey{
ID: "id-1",
Key: "key-1",
Name: "name-1",
Permissions: account.PermissionsMap{},
}
k := &account.APIKey{
ID: "id-1",
Key: "key-1",
Name: "name-1",
Permissions: account.PermissionsMap{},
}

_, err := c.APIKeys.Create(k)
require.NoError(t, err)
})
_, err := c.APIKeys.Create(k)
require.NoError(t, err)
}

func TestCreateDDIAPIKey(t *testing.T) {
Expand Down
16 changes: 0 additions & 16 deletions rest/account_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ func (s *TeamsService) Create(t *account.Team) (*http.Response, error) {
err error
)

// Replace `manage_jobs` with the new split permissions
if t != nil && t.Permissions.Monitoring.ManageJobs {
t.Permissions.Monitoring.ManageJobs = false
t.Permissions.Monitoring.CreateJobs = true
t.Permissions.Monitoring.UpdateJobs = true
t.Permissions.Monitoring.DeleteJobs = true
}

// If this is DDI then the permissions need to be transformed to DDI-compatible permissions.
if s.client.DDI && t != nil {
ddiTeam := teamToDDITeam(t)
Expand Down Expand Up @@ -112,14 +104,6 @@ func (s *TeamsService) Update(t *account.Team) (*http.Response, error) {
err error
)

// Replace `manage_jobs` with the new split permissions
if t != nil && t.Permissions.Monitoring.ManageJobs {
t.Permissions.Monitoring.ManageJobs = false
t.Permissions.Monitoring.CreateJobs = true
t.Permissions.Monitoring.UpdateJobs = true
t.Permissions.Monitoring.DeleteJobs = true
}

// If this is DDI then the permissions need to be transformed to DDI-compatible permissions.
if s.client.DDI && t != nil {
ddiTeam := teamToDDITeam(t)
Expand Down
76 changes: 22 additions & 54 deletions rest/account_team_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,65 +14,33 @@ import (
)

func TestCreateTeam(t *testing.T) {
t.Run("with manage_jobs set to true", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)

var tm account.Team
require.NoError(t, json.Unmarshal(b, &tm))
assert.Nil(t, tm.Permissions.Security)
assert.Nil(t, tm.Permissions.DHCP)
assert.Nil(t, tm.Permissions.IPAM)
assert.False(t, tm.Permissions.Monitoring.ManageJobs)
assert.True(t, tm.Permissions.Monitoring.CreateJobs)
assert.True(t, tm.Permissions.Monitoring.UpdateJobs)
assert.True(t, tm.Permissions.Monitoring.DeleteJobs)

w.Write(b)
}))
defer ts.Close()
c := NewClient(nil, SetEndpoint(ts.URL))

tm := &account.Team{
ID: "id-1",
Name: "team-1",
Permissions: account.PermissionsMap{Monitoring: account.PermissionsMonitoring{ManageJobs: true}},
}

_, err := c.Teams.Create(tm)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
})

t.Run("with manage_jobs not set", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)

var tm account.Team
require.NoError(t, json.Unmarshal(b, &tm))
assert.Nil(t, tm.Permissions.Security)
assert.Nil(t, tm.Permissions.DHCP)
assert.Nil(t, tm.Permissions.IPAM)
assert.False(t, tm.Permissions.Monitoring.ManageJobs)
assert.False(t, tm.Permissions.Monitoring.CreateJobs)
assert.False(t, tm.Permissions.Monitoring.UpdateJobs)
assert.False(t, tm.Permissions.Monitoring.DeleteJobs)
var tm account.Team
require.NoError(t, json.Unmarshal(b, &tm))
assert.Nil(t, tm.Permissions.Security)
assert.Nil(t, tm.Permissions.DHCP)
assert.Nil(t, tm.Permissions.IPAM)
assert.False(t, tm.Permissions.Monitoring.ManageJobs)
assert.False(t, tm.Permissions.Monitoring.CreateJobs)
assert.False(t, tm.Permissions.Monitoring.UpdateJobs)
assert.False(t, tm.Permissions.Monitoring.DeleteJobs)

w.Write(b)
}))
defer ts.Close()
c := NewClient(nil, SetEndpoint(ts.URL))
w.Write(b)
}))
defer ts.Close()
c := NewClient(nil, SetEndpoint(ts.URL))

tm := &account.Team{
ID: "id-1",
Name: "team-1",
Permissions: account.PermissionsMap{},
}
tm := &account.Team{
ID: "id-1",
Name: "team-1",
Permissions: account.PermissionsMap{},
}

_, err := c.Teams.Create(tm)
require.NoError(t, err)
})
_, err := c.Teams.Create(tm)
require.NoError(t, err)
}

func TestCreateDDITeam(t *testing.T) {
Expand Down
16 changes: 0 additions & 16 deletions rest/account_user.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ func (s *UsersService) Create(u *account.User) (*http.Response, error) {
err error
)

// Replace `manage_jobs` with the new split permissions
if u != nil && u.Permissions.Monitoring.ManageJobs {
u.Permissions.Monitoring.ManageJobs = false
u.Permissions.Monitoring.CreateJobs = true
u.Permissions.Monitoring.UpdateJobs = true
u.Permissions.Monitoring.DeleteJobs = true
}

// If this is DDI then the permissions need to be transformed to DDI-compatible permissions.
if s.client.DDI && u != nil {
ddiUser := userToDDIUser(u)
Expand Down Expand Up @@ -112,14 +104,6 @@ func (s *UsersService) Update(u *account.User) (*http.Response, error) {
err error
)

// Replace `manage_jobs` with the new split permissions
if u != nil && u.Permissions.Monitoring.ManageJobs {
u.Permissions.Monitoring.ManageJobs = false
u.Permissions.Monitoring.CreateJobs = true
u.Permissions.Monitoring.UpdateJobs = true
u.Permissions.Monitoring.DeleteJobs = true
}

// If this is DDI then the permissions need to be transformed to DDI-compatible permissions.
if s.client.DDI && u != nil {
ddiUser := userToDDIUser(u)
Expand Down
79 changes: 23 additions & 56 deletions rest/account_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,67 +14,34 @@ import (
)

func TestCreateUser(t *testing.T) {
t.Run("with manage_jobs set to true", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)

var u account.User
require.NoError(t, json.Unmarshal(b, &u))
assert.Nil(t, u.Permissions.Security)
assert.Nil(t, u.Permissions.DHCP)
assert.Nil(t, u.Permissions.IPAM)
assert.False(t, u.Permissions.Monitoring.ManageJobs)
assert.True(t, u.Permissions.Monitoring.CreateJobs)
assert.True(t, u.Permissions.Monitoring.UpdateJobs)
assert.True(t, u.Permissions.Monitoring.DeleteJobs)

w.Write(b)
}))
defer ts.Close()
c := NewClient(nil, SetEndpoint(ts.URL))

u := &account.User{
Name: "name-1",
Username: "user-1",
Email: "email-1",
Permissions: account.PermissionsMap{Monitoring: account.PermissionsMonitoring{ManageJobs: true}},
}

_, err := c.Users.Create(u)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)
})

t.Run("with manage_jobs not set", func(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
b, err := ioutil.ReadAll(r.Body)
require.NoError(t, err)

var u account.User
require.NoError(t, json.Unmarshal(b, &u))
assert.Nil(t, u.Permissions.Security)
assert.Nil(t, u.Permissions.DHCP)
assert.Nil(t, u.Permissions.IPAM)
assert.False(t, u.Permissions.Monitoring.ManageJobs)
assert.False(t, u.Permissions.Monitoring.CreateJobs)
assert.False(t, u.Permissions.Monitoring.UpdateJobs)
assert.False(t, u.Permissions.Monitoring.DeleteJobs)
var u account.User
require.NoError(t, json.Unmarshal(b, &u))
assert.Nil(t, u.Permissions.Security)
assert.Nil(t, u.Permissions.DHCP)
assert.Nil(t, u.Permissions.IPAM)
assert.False(t, u.Permissions.Monitoring.ManageJobs)
assert.False(t, u.Permissions.Monitoring.CreateJobs)
assert.False(t, u.Permissions.Monitoring.UpdateJobs)
assert.False(t, u.Permissions.Monitoring.DeleteJobs)

w.Write(b)
}))
defer ts.Close()
c := NewClient(nil, SetEndpoint(ts.URL))
w.Write(b)
}))
defer ts.Close()
c := NewClient(nil, SetEndpoint(ts.URL))

u := &account.User{
Name: "name-1",
Username: "user-1",
Email: "email-1",
Permissions: account.PermissionsMap{},
}
u := &account.User{
Name: "name-1",
Username: "user-1",
Email: "email-1",
Permissions: account.PermissionsMap{},
}

_, err := c.Users.Create(u)
require.NoError(t, err)
})
_, err := c.Users.Create(u)
require.NoError(t, err)
}

func TestCreateDDIUser(t *testing.T) {
Expand Down

0 comments on commit e316263

Please sign in to comment.