some tests and major cleanup

This commit is contained in:
ston1th 2018-10-19 23:08:32 +02:00
commit 16671b3406
30 changed files with 192057 additions and 202431 deletions

View file

@ -11,18 +11,6 @@ const (
Private
)
func ParsePerm(p int) Permission {
switch p {
case 1:
return Public
case 2:
return Internal
case 3:
return Private
}
return Invalid
}
func ParsePermString(p string) Permission {
switch p {
case "1":
@ -51,10 +39,6 @@ func WritePerm(username, section string, p *Page) bool {
if section != WikiSection && section != username {
return false
}
return writePerm(username, p)
}
func writePerm(username string, p *Page) bool {
switch p.Perm {
case Public, Internal:
if p.Owner == WikiSection {

View file

@ -0,0 +1,81 @@
// Copyright (C) 2018 Marius Schellenberger
package core
import (
"fmt"
"testing"
)
func TestParsePermString(t *testing.T) {
data := map[string]Permission{
"": Invalid,
"1": Public,
"2": Internal,
"3": Private,
"123": Invalid,
"test": Invalid,
}
for k, v := range data {
t.Run(k, func(t *testing.T) {
if ParsePermString(k) != v {
t.Fail()
}
})
}
}
func TestReadPerm(t *testing.T) {
data := []struct {
Username string
Page *Page
OK bool
}{
{"", &Page{Perm: Invalid, Owner: "admin"}, false},
{"", &Page{Perm: Public, Owner: "admin"}, true},
{"user", &Page{Perm: Public, Owner: "admin"}, true},
{"", &Page{Perm: Internal, Owner: "admin"}, false},
{"user", &Page{Perm: Internal, Owner: "admin"}, true},
{"", &Page{Perm: Private, Owner: "admin"}, false},
{"user", &Page{Perm: Private, Owner: "admin"}, false},
{"admin", &Page{Perm: Private, Owner: "admin"}, true},
}
for i, v := range data {
t.Run(fmt.Sprintf("Read:%d", i), func(t *testing.T) {
if ReadPerm(v.Username, v.Page) != v.OK {
t.Fail()
}
})
}
}
func TestWritePerm(t *testing.T) {
data := []struct {
Username string
Section string
Page *Page
OK bool
}{
{"user", "admin", &Page{Perm: Invalid, Owner: "admin"}, false},
{"", "user", &Page{Perm: Invalid, Owner: "admin"}, false},
{"user", "user", &Page{Perm: Invalid, Owner: "user"}, false},
{"user", "user", &Page{Perm: Public, Owner: "user"}, true},
{"user", "user", &Page{Perm: Internal, Owner: "user"}, true},
{"user", "user", &Page{Perm: Private, Owner: "user"}, true},
{"", WikiSection, &Page{Perm: Invalid, Owner: "admin"}, false},
{"", WikiSection, &Page{Perm: Public, Owner: "admin"}, false},
{"", WikiSection, &Page{Perm: Internal, Owner: "admin"}, false},
{"user", WikiSection, &Page{Perm: Public, Owner: WikiSection}, true},
{"user", WikiSection, &Page{Perm: Internal, Owner: WikiSection}, true},
{"", "admin", &Page{Perm: Private, Owner: "admin"}, false},
{"user", "admin", &Page{Perm: Private, Owner: "admin"}, false},
{"admin", "admin", &Page{Perm: Private, Owner: "admin"}, true},
}
for i, v := range data {
t.Run(fmt.Sprintf("Write:%d", i), func(t *testing.T) {
if WritePerm(v.Username, v.Section, v.Page) != v.OK {
t.Fail()
}
})
}
}

7
pkg/core/pledge.go Normal file
View file

@ -0,0 +1,7 @@
// Copyright (C) 2018 Marius Schellenberger
// +build !openbsd
package core
func Pledge(promises, execpromises string) error { return nil }

View file

@ -0,0 +1,11 @@
// Copyright (C) 2018 Marius Schellenberger
// +build openbsd
package core
import "golang.org/x/sys/unix"
func Pledge(promises, execpromises string) error {
return unix.Pledge(promises, execpromises)
}

View file

@ -8,7 +8,7 @@ type Page struct {
Title string `json:"title"`
StoreTitle string `json:"store_title"`
Markdown string `json:"markdown"`
PageIndex template.HTML `json:"page_index"`
TOC template.HTML `json:"toc"`
HTML template.HTML `json:"html"`
Created string `json:"created"`
Updated string `json:"updated"`
@ -52,4 +52,5 @@ type Config struct {
Secret string
Foreground bool
SecureCookie bool
Debug bool
}