diff --git a/gandalftest/server.go b/gandalftest/server.go index 9120467..27a1be1 100644 --- a/gandalftest/server.go +++ b/gandalftest/server.go @@ -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() diff --git a/gandalftest/server_test.go b/gandalftest/server_test.go index bc688cd..76c2efa 100644 --- a/gandalftest/server_test.go +++ b/gandalftest/server_test.go @@ -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)