HTTP acceptance testing
This commit is contained in:
parent
5d6db07758
commit
e62f7f5840
1 changed files with 172 additions and 0 deletions
172
http_test.go
Normal file
172
http_test.go
Normal file
|
@ -0,0 +1,172 @@
|
|||
package megauploader
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"flag"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"testing"
|
||||
)
|
||||
|
||||
var (
|
||||
doHTTP = flag.Bool("http-tests", false, "run HTTP integration tests")
|
||||
)
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
flag.Parse()
|
||||
result := m.Run()
|
||||
os.Exit(result)
|
||||
}
|
||||
|
||||
func getServer(cfg string) *httptest.Server {
|
||||
conf, err := ParseConf([]byte(cfg))
|
||||
if err != nil {
|
||||
panic("Error parsing conf in getServer")
|
||||
}
|
||||
mu := NewMegaUploader(conf)
|
||||
ts := httptest.NewServer(mu.SetupRoutes())
|
||||
return ts
|
||||
}
|
||||
|
||||
// ask home page without authentication; 401 expected
|
||||
func TestAuthDeny(t *testing.T) {
|
||||
if !*doHTTP {
|
||||
return
|
||||
}
|
||||
ts := getServer(``)
|
||||
defer ts.Close()
|
||||
resp, err := ts.Client().Get(ts.URL)
|
||||
if err != nil {
|
||||
t.Fatalf("Error asking home to test server")
|
||||
return
|
||||
}
|
||||
if resp.StatusCode != 401 {
|
||||
t.Errorf("Viewing home should require authentication; got `%s` instead",
|
||||
resp.Status,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
// ask for non-existing page without authentication; 404 expected
|
||||
func TestNotFoundWhenUnauthenticated(t *testing.T) {
|
||||
if !*doHTTP {
|
||||
return
|
||||
}
|
||||
ts := getServer(``)
|
||||
defer ts.Close()
|
||||
resp, err := ts.Client().Get(ts.URL + "/dontexist")
|
||||
if err != nil {
|
||||
t.Fatalf("Error asking non-existent URL to test server: \n%s\n", err)
|
||||
return
|
||||
}
|
||||
if resp.StatusCode != 404 {
|
||||
t.Errorf("Asked not existing endpoint, expected 404; got `%s` instead",
|
||||
resp.Status,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func userRequest(url, user string, t *testing.T) *http.Request {
|
||||
req, err := http.NewRequest("GET", url, nil)
|
||||
if err != nil {
|
||||
t.Fatalf("error preparing request: \n%s\n", err)
|
||||
return nil
|
||||
}
|
||||
req.Header.Add("X-Forwarded-User", user)
|
||||
return req
|
||||
}
|
||||
|
||||
// ask for home with valid user
|
||||
func TestAuthOk(t *testing.T) {
|
||||
if !*doHTTP {
|
||||
return
|
||||
}
|
||||
ts := getServer(``)
|
||||
defer ts.Close()
|
||||
cl := ts.Client()
|
||||
req := userRequest(ts.URL, "someone", t)
|
||||
resp, err := cl.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Error asking home to test server")
|
||||
return
|
||||
}
|
||||
if resp.StatusCode != 200 {
|
||||
t.Errorf("Viewing home should require authentication; got `%s` instead",
|
||||
resp.Status,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func getShareList(ts *httptest.Server, user string, t *testing.T) []Share {
|
||||
req := userRequest(ts.URL+"/api/share", user, t)
|
||||
cl := ts.Client()
|
||||
resp, err := cl.Do(req)
|
||||
if err != nil {
|
||||
t.Fatalf("Error asking share list to test server: \n%s\n", err)
|
||||
return []Share{}
|
||||
}
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
t.Fatal("Error reading response", err)
|
||||
return []Share{}
|
||||
}
|
||||
var shares []Share
|
||||
err = json.Unmarshal(body, &shares)
|
||||
if err != nil {
|
||||
t.Fatal("Invalid JSON received", err)
|
||||
return []Share{}
|
||||
}
|
||||
return shares
|
||||
}
|
||||
|
||||
// share list with invalid user
|
||||
func TestShareRejected(t *testing.T) {
|
||||
if !*doHTTP {
|
||||
return
|
||||
}
|
||||
ts := getServer(`
|
||||
global:
|
||||
excluded:
|
||||
- john@doe.us
|
||||
- foo bar
|
||||
shares:
|
||||
- name: foo
|
||||
authorized: ["*"]
|
||||
description: foo
|
||||
`)
|
||||
defer ts.Close()
|
||||
shares := getShareList(ts, "john@doe.us", t)
|
||||
if len(shares) != 0 {
|
||||
t.Fatal("Asked share list with banned user, expected empty, got", shares)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// share list with valid user
|
||||
func TestShareOk(t *testing.T) {
|
||||
if !*doHTTP {
|
||||
return
|
||||
}
|
||||
ts := getServer(`
|
||||
global:
|
||||
excluded:
|
||||
- john@doe.us
|
||||
- foo bar
|
||||
shares:
|
||||
- name: foo
|
||||
authorized: ["*"]
|
||||
description: foo
|
||||
`)
|
||||
defer ts.Close()
|
||||
shares := getShareList(ts, "someone elese", t)
|
||||
if len(shares) == 0 {
|
||||
t.Fatal("Asked share list with not banned user, expected [foo], got", shares)
|
||||
return
|
||||
}
|
||||
if shares[0].Name != "foo" {
|
||||
t.Error("The only share should be `foo`, got", shares[0].Name)
|
||||
return
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue