From 667d07636ca905e10bec1b689888b43b6b6e155a Mon Sep 17 00:00:00 2001 From: Umputun Date: Mon, 12 Mar 2018 19:18:00 -0500 Subject: [PATCH] more rest tests --- app/main.go | 2 ++ app/rest/api/rest.go | 7 +++---- app/rest/api/rest_test.go | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 4 deletions(-) diff --git a/app/main.go b/app/main.go index 74d75bdd..c496fa83 100644 --- a/app/main.go +++ b/app/main.go @@ -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, diff --git a/app/rest/api/rest.go b/app/rest/api/rest.go index cee0339a..38d22439 100644 --- a/app/rest/api/rest.go +++ b/app/rest/api/rest.go @@ -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() diff --git a/app/rest/api/rest_test.go b/app/rest/api/rest_test.go index 1d3c99e2..3c15700f 100644 --- a/app/rest/api/rest_test.go +++ b/app/rest/api/rest_test.go @@ -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{}