more rest tests
This commit is contained in:
@@ -46,6 +46,7 @@ var opts struct {
|
||||
AvatarStore string `long:"avatars" env:"AVATAR_STORE" default:"./var/avatars" description:"path to avatars directory"`
|
||||
DefaultAvatar string `long:"avatar-def" env:"AVATAR_DEF" default:"remark.image" description:"default avatar"`
|
||||
Port int `long:"port" env:"REMARK_PORT" default:"8080" description:"port"`
|
||||
WebRoot string `long:"web-root" env:"REMARK_WEB_ROOT" default:"./web" description:"web root directory"`
|
||||
} `command:"server" description:"run server"`
|
||||
|
||||
ImportCommand struct {
|
||||
@@ -116,6 +117,7 @@ func main() {
|
||||
Version: revision,
|
||||
DataService: dataService,
|
||||
Exporter: exporter,
|
||||
WebRoot: opts.ServerCommand.WebRoot,
|
||||
Authenticator: auth.Authenticator{
|
||||
Admins: opts.Admins,
|
||||
SessionStore: sessionStore,
|
||||
|
||||
@@ -8,7 +8,6 @@ import (
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
@@ -30,12 +29,12 @@ import (
|
||||
|
||||
// Rest is a rest access server
|
||||
type Rest struct {
|
||||
Version string
|
||||
|
||||
Version string
|
||||
DataService store.Service
|
||||
Authenticator auth.Authenticator
|
||||
Exporter migrator.Exporter
|
||||
Cache rest.LoadingCache
|
||||
WebRoot string
|
||||
|
||||
httpServer *http.Server
|
||||
mod admin
|
||||
@@ -108,7 +107,7 @@ func (s *Rest) Run(port int) {
|
||||
router.Get("/robots.txt", func(w http.ResponseWriter, r *http.Request) {
|
||||
render.PlainText(w, r, "User-agent: *\nDisallow: /auth/\nDisallow: /api/\n")
|
||||
})
|
||||
addFileServer(router, "/web", http.Dir(filepath.Join(".", "web")))
|
||||
addFileServer(router, "/web", http.Dir(s.WebRoot))
|
||||
|
||||
s.httpServer = &http.Server{Addr: fmt.Sprintf(":%d", port), Handler: router}
|
||||
err := s.httpServer.ListenAndServe()
|
||||
|
||||
@@ -23,6 +23,7 @@ import (
|
||||
)
|
||||
|
||||
var testDb = "/tmp/test-remark.db"
|
||||
var testHtml = "/tmp/test-remark.html"
|
||||
|
||||
func TestServer_Ping(t *testing.T) {
|
||||
srv, port := prep(t)
|
||||
@@ -382,6 +383,31 @@ func TestServer_List(t *testing.T) {
|
||||
assert.Equal(t, []store.PostInfo{{URL: "https://radio-t.com/blah2", Count: 2}, {URL: "https://radio-t.com/blah1", Count: 3}}, pi)
|
||||
}
|
||||
|
||||
func TestServer_Config(t *testing.T) {
|
||||
srv, port := prep(t)
|
||||
assert.NotNil(t, srv)
|
||||
defer cleanup(srv)
|
||||
|
||||
body, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/api/v1/config?site=radio-t", port))
|
||||
assert.Equal(t, 200, code)
|
||||
j := JSON{}
|
||||
err := json.Unmarshal([]byte(body), &j)
|
||||
assert.Nil(t, err)
|
||||
assert.Equal(t, 300., j["edit_duration"])
|
||||
assert.EqualValues(t, []interface{}([]interface{}{"a1", "a2"}), j["admins"])
|
||||
t.Logf("%+v", j)
|
||||
}
|
||||
|
||||
func TestServer_FileServer(t *testing.T) {
|
||||
srv, port := prep(t)
|
||||
assert.NotNil(t, srv)
|
||||
defer cleanup(srv)
|
||||
|
||||
body, code := get(t, fmt.Sprintf("http://127.0.0.1:%d/web/test-remark.html", port))
|
||||
assert.Equal(t, 200, code)
|
||||
assert.Equal(t, "some html", body)
|
||||
}
|
||||
|
||||
func prep(t *testing.T) (srv *Rest, port int) {
|
||||
dataStore, err := store.NewBoltDB(store.BoltSite{FileName: testDb, SiteID: "radio-t"})
|
||||
require.Nil(t, err)
|
||||
@@ -393,14 +419,22 @@ func prep(t *testing.T) (srv *Rest, port int) {
|
||||
DevPasswd: "password",
|
||||
Providers: nil,
|
||||
AvatarProxy: &auth.AvatarProxy{StorePath: "/tmp", RoutePath: "/api/v1/avatar"},
|
||||
Admins: []string{"a1", "a2"},
|
||||
},
|
||||
Exporter: &migrator.Remark{DataStore: dataStore},
|
||||
Cache: &mockCache{},
|
||||
WebRoot: "/tmp",
|
||||
}
|
||||
|
||||
ioutil.WriteFile(testHtml, []byte("some html"), 0700)
|
||||
portSetCh := make(chan bool)
|
||||
go func() {
|
||||
port = rand.Intn(50000) + 1025
|
||||
portSetCh <- true
|
||||
srv.Run(port)
|
||||
}()
|
||||
<-portSetCh
|
||||
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
return srv, port
|
||||
}
|
||||
@@ -435,6 +469,7 @@ func cleanup(srv *Rest) {
|
||||
srv.httpServer.Close()
|
||||
srv.httpServer.Shutdown(context.Background())
|
||||
os.Remove(testDb)
|
||||
os.Remove(testHtml)
|
||||
}
|
||||
|
||||
type mockCache struct{}
|
||||
|
||||
Reference in New Issue
Block a user