Skip to content
This repository has been archived by the owner on Apr 26, 2021. It is now read-only.

Commit

Permalink
gandalftest: add Grants and ReadOnlyGrants to get grants in the server
Browse files Browse the repository at this point in the history
  • Loading branch information
Francisco Souza committed Feb 10, 2015
1 parent fd26753 commit c850cc8
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
24 changes: 24 additions & 0 deletions gandalftest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,30 @@ func (s *GandalfServer) Repositories() []Repository {
return s.repos
}

// Grants returns a map of grant in repositories, mapping the name of the
// repository to the slice of users that have access to it.
func (s *GandalfServer) Grants() map[string][]string {
s.repoLock.RLock()
defer s.repoLock.RUnlock()
result := make(map[string][]string, len(s.repos))
for _, repo := range s.repos {
result[repo.Name] = repo.Users
}
return result
}

// ReadOnlyGrants returns a map of read-only grants in repositories, mapping
// the name of the repository to the slice of users that have access to it.
func (s *GandalfServer) ReadOnlyGrants() map[string][]string {
s.repoLock.RLock()
defer s.repoLock.RUnlock()
result := make(map[string][]string, len(s.repos))
for _, repo := range s.repos {
result[repo.Name] = repo.ReadOnlyUsers
}
return result
}

// Reset resets all internal information of the server, like keys, repositories, users and prepared failures.
func (s *GandalfServer) Reset() {
s.usersLock.Lock()
Expand Down
30 changes: 30 additions & 0 deletions gandalftest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,36 @@ func (s *S) TestRepositories(c *check.C) {
c.Assert(server.Repositories(), check.DeepEquals, server.repos)
}

func (s *S) TestGrants(c *check.C) {
server, err := NewServer("127.0.0.1:0")
c.Assert(err, check.IsNil)
defer server.Stop()
server.repos = []Repository{
{Name: "something", Users: []string{"user1", "user2"}},
{Name: "otherthing", Users: []string{"user2", "user3"}},
}
expected := map[string][]string{
"something": {"user1", "user2"},
"otherthing": {"user2", "user3"},
}
c.Assert(server.Grants(), check.DeepEquals, expected)
}

func (s *S) TestReadOnlyGrants(c *check.C) {
server, err := NewServer("127.0.0.1:0")
c.Assert(err, check.IsNil)
defer server.Stop()
server.repos = []Repository{
{Name: "something", ReadOnlyUsers: []string{"user1", "user2"}},
{Name: "otherthing", ReadOnlyUsers: []string{"user2", "user3"}},
}
expected := map[string][]string{
"something": {"user1", "user2"},
"otherthing": {"user2", "user3"},
}
c.Assert(server.ReadOnlyGrants(), check.DeepEquals, expected)
}

func (s *S) TestReset(c *check.C) {
server, err := NewServer("127.0.0.1:0")
c.Assert(err, check.IsNil)
Expand Down

0 comments on commit c850cc8

Please sign in to comment.