possibility to open repo in single user mode. #1

This commit is contained in:
Niko Abeler 2022-08-03 20:34:42 +02:00
parent a08740dec4
commit 910e74682f
3 changed files with 60 additions and 2 deletions

View File

@ -21,6 +21,8 @@ var VERSION = "0.0.1"
type Repository struct {
name string
single_user_mode bool
active_user string
}
func CreateRepository(name string) (Repository, error) {
@ -58,7 +60,16 @@ func OpenRepository(name string) (Repository, error) {
}
return repo, nil
}
func OpenSingleUserRepo(name string, user_name string) (Repository, error) {
repo, err := OpenRepository(name)
if err != nil {
return Repository{}, err
}
repo.single_user_mode = true
repo.active_user = user_name
return repo, nil
}
func (repo Repository) Dir() string {
@ -73,6 +84,13 @@ func (repo Repository) UsersDir() string {
return path.Join(repo.Dir(), "users")
}
func (repo Repository) UserUrlPath(user User) string {
if repo.single_user_mode {
return "/"
}
return "/user/" + user.name + "/"
}
func (repo Repository) Template() (string, error) {
// load base.html
path := path.Join(repo.Dir(), "base.html")
@ -84,6 +102,10 @@ func (repo Repository) Template() (string, error) {
}
func (repo Repository) Users() ([]User, error) {
if repo.single_user_mode {
return []User{{repo: repo, name: repo.active_user}}, nil
}
userNames := listDir(repo.UsersDir())
users := make([]User, len(userNames))
for i, name := range userNames {

View File

@ -187,3 +187,39 @@ func TestCanGetRepoTemplate(t *testing.T) {
t.Error("Template not returned")
}
}
func TestCanOpenRepositoryInSingleUserMode(t *testing.T) {
// Create a new user
repoName := testRepoName()
userName := randomUserName()
created_repo, _ := owl.CreateRepository(repoName)
created_repo.CreateUser(userName)
created_repo.CreateUser(randomUserName())
created_repo.CreateUser(randomUserName())
// Open the repository
repo, _ := owl.OpenSingleUserRepo(repoName, userName)
users, _ := repo.Users()
if len(users) != 1 {
t.Error("Wrong number of users returned, expected 1, got ", len(users))
}
if users[0].Name() != userName {
t.Error("User name does not match")
}
}
func TestSingleUserRepoUserUrlPathIsSimple(t *testing.T) {
// Create a new user
repoName := testRepoName()
userName := randomUserName()
created_repo, _ := owl.CreateRepository(repoName)
created_repo.CreateUser(userName)
// Open the repository
repo, _ := owl.OpenSingleUserRepo(repoName, userName)
user, _ := repo.GetUser(userName)
if user.UrlPath() != "/" {
t.Error("User url is not '/'. Got: ", user.UrlPath())
}
}

View File

@ -26,7 +26,7 @@ func (user User) Dir() string {
}
func (user User) UrlPath() string {
return "/user/" + user.name + "/"
return user.repo.UserUrlPath(user)
}
func (user User) PostDir() string {