示例#1
0
func newTestServer(t *testing.T) (*httptest.Server, chan bool) {
	done := make(chan bool)
	tmpd, err := ioutil.TempDir("/tmp", "erickson")
	if err != nil {
		t.Fatal(err)
	}
	go func() {
		<-done
		if err := os.RemoveAll(tmpd); err != nil {
			t.Fatal(err)
		}
	}()
	db, err := db.NewBoltDB(tmpd + "/erickson.db")
	if err != nil {
		t.Fatal(err)
	}
	u, err := review.NewUser("bob", "supersecret")
	if err != nil {
		t.Fatal(err)
	}
	if err := db.CreateUser(u); err != nil {
		t.Fatal(err)
	}
	key := []byte("12345678901234567890123456789012")
	return httptest.NewServer(server.NewRootHandler(db, tmpd, key, mail.Nil, "")), done
}
示例#2
0
func TestServer(t *testing.T) {
	f, err := ioutil.TempFile("/tmp", "erickson")
	if err != nil {
		t.Fatal(err)
	}
	defer os.Remove(f.Name())

	db, err := db.NewBoltDB(f.Name())
	if err != nil {
		t.Fatal(err)
	}

	user, err := review.NewUser("testuser", "testpassword")
	if err != nil {
		t.Fatal(err)
	}
	if err := db.CreateUser(user); err != nil {
		t.Fatal(err)
	}

	wd, err := os.Getwd()
	if err != nil {
		t.Fatal(err)
	}
	handler := NewRootHandler(db, wd+"./..", []byte("12345678901234567890123456789012"), mail.Nil, "")

	server := httptest.NewUnstartedServer(handler)
	cert, err := tls.LoadX509KeyPair("test.crt", "test.key")
	if err != nil {
		t.Fatal(err)
	}
	tlsConfig := &tls.Config{
		InsecureSkipVerify: true, // testing only
		Certificates:       []tls.Certificate{cert},
	}
	server.TLS = tlsConfig
	server.StartTLS()
	defer server.Close()

	*handler.URL = server.URL
	url := server.URL

	client := newClient(t, url)
	url = create(t, client, url)
	read(t, client, url)
	annotate(t, client, url+"/rev/0")
	update(t, client, url)
	destroy(t, client, url)
}
示例#3
0
文件: auth.go 项目: echlebek/erickson
func postSignup(ctx context, w http.ResponseWriter, req *http.Request) {
	if err := req.ParseForm(); err != nil {
		log.Println(err)
		http.Error(w, "couldn't parse form", 400)
		return
	}
	username := req.FormValue("username")
	password := req.FormValue("password")

	user, err := review.NewUser(username, password)
	if err != nil {
		log.Println(err)
		http.Error(w, "couldn't create user", 500)
		return
	}
	if err := ctx.db.CreateUser(user); err != nil {
		log.Println(err)
		http.Error(w, "couldn't create user", 500)
		return
	}

	http.Redirect(w, req, "/", http.StatusSeeOther)
}