Beispiel #1
0
func postFormReview(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
	}
	session, err := ctx.store.Get(req, SessionName)
	if err != nil {
		log.Println(err)
		http.Error(w, err500, 500)
		return
	}
	username, ok := session.Values["username"].(string)
	if !ok {
		log.Println("session doesn't contain a valid username")
		http.Error(w, err500, 500)
		return
	}
	raw := req.FormValue("diff")
	files, err := diff.ParseFiles(raw)
	if err != nil {
		log.Println(err)
		http.Error(w, "couldn't parse diff", http.StatusBadRequest)
		return
	}
	r := review.R{
		Summary: review.Summary{
			CommitMsg:   req.FormValue("commitmsg"),
			Submitter:   username,
			Repository:  req.FormValue("repository"),
			SubmittedAt: time.Now(),
			Status:      review.Open,
		},
		Revisions: []review.Revision{
			{Files: files},
		},
	}
	id, err := ctx.db.CreateReview(r)
	if err != nil {
		log.Println(err)
		http.Error(w, "couldn't create review", 500)
		return
	}
	ctx.review = id
	url := ctx.reviewURL()
	http.Redirect(w, req, url, 303)
}
Beispiel #2
0
func init() {
	files, err := diff.ParseFiles(mockDiff1)
	if err != nil {
		panic(err)
	}
	mockReview = review.R{
		Summary: review.Summary{
			Submitter:   "eric",
			SubmittedAt: time.Now(),
			UpdatedAt:   time.Now(),
		},
		Revisions: []review.Revision{
			{
				Files: files,
				Annotations: []review.Annotation{
					{File: 0, Hunk: 0, Line: 0, Comment: "foo"},
				},
			},
		},
	}
}
Beispiel #3
0
func create(t *testing.T, client *http.Client, host string) string {
	files, err := diff.ParseFiles(diffTxt)
	if err != nil {
		t.Fatal(err)
	}
	r := review.R{
		Summary:   review.Summary{},
		Revisions: []review.Revision{{Files: files}},
	}
	b, err := json.Marshal(r)
	if err != nil {
		t.Fatal(err)
	}
	req, err := http.NewRequest("POST", host+"/reviews", bytes.NewReader(b))
	if err != nil {
		t.Fatal(err)
	}
	req.Header.Set("Content-Type", "application/json")
	resp, err := client.Do(req)
	// The server issues a redirect to the review, so the status ends up being 200.
	if err != nil || resp.StatusCode != 200 {
		t.Fatalf("bad response: %d, %s", resp.StatusCode, err)
	}

	// Now do the same thing with application/x-www-form-urlencoded
	data := url.Values{
		"diff":       {diffTxt},
		"commitmsg":  {"Changed some things"},
		"username":   {"strands slurdinand"},
		"repository": {"boring_tools"},
	}
	if resp, err := client.PostForm(host+"/reviews", data); err != nil || resp.StatusCode != 200 {
		t.Fatalf("bad response: %d, %s", resp.StatusCode, err)
	}

	return host + "/reviews/1"
}
Beispiel #4
0
// Test an entire review lifecycle.
func TestCRUD(t *testing.T) {
	tmpdir, err := ioutil.TempDir("/tmp", "erickson")
	if err != nil {
		t.Fatal(err)
	}
	defer os.RemoveAll(tmpdir)
	db, err := NewBoltDB(tmpdir + "/erickson.db")
	if err != nil {
		t.Fatal(err)
	}
	id, err := db.CreateReview(mockReview)
	if err != nil {
		t.Error(db.String())
		t.Error(db.GoString())
		t.Fatal(err)
	}
	mockReview.Summary.ID = id
	if exp := 1; id != exp {
		t.Errorf("wrong review id. got %d, want %d", id, exp)
	}

	gotReview, err := db.GetReview(1)
	if err != nil {
		t.Fatal(err)
	}

	if s1, s2 := gotReview.Summary, mockReview.Summary; !summaryEq(s1, s2) {
		t.Errorf("bad summary data. got %#v, want %#v", s1, s2)
	}

	if r1, r2 := gotReview.Revisions, mockReview.Revisions; !revisionsEq(r1, r2) {
		t.Errorf("bad revision data. got %#v, want %#v", r1, r2)
	}

	mockReview2 := mockReview

	id, err = db.CreateReview(mockReview2)
	if err != nil {
		t.Error(db.String())
		t.Error(db.GoString())
		t.Fatal(err)
	}
	mockReview2.Summary.ID = id
	if exp := 2; id != exp {
		t.Errorf("wrong review id. got %d, want %d", id, exp)
	}
	files, err := diff.ParseFiles(mockDiff2)
	if err != nil {
		t.Fatal(err)
	}
	if err := db.AddRevision(2, review.Revision{Files: files}); err != nil {
		t.Fatal(err)
	}
	gotReview, err = db.GetReview(2)
	if err != nil {
		t.Error(db.String())
		t.Error(db.GoString())
		t.Fatal(err)
	}

	mockReview2.Revisions = append(mockReview2.Revisions, review.Revision{Files: files})

	if s1, s2 := gotReview.Summary, mockReview2.Summary; !summaryEq(s1, s2) {
		t.Errorf("bad summary data. got %#v, want %#v", s1, s2)
	}

	if r1, r2 := gotReview.Revisions, mockReview2.Revisions; !revisionsEq(r1, r2) {
		t.Errorf("bad revision data. got %#v, want %#v", r1, r2)
	}

	summaries, err := db.GetSummaries()
	if err != nil {
		t.Fatal(err)
	}
	if sumLen := len(summaries); sumLen != 2 {
		t.Fatalf("wrong number of review summaries: got %d, want 2", sumLen)
	}
	if !summaryEq(summaries[0], mockReview.Summary) {
		t.Errorf("wrong review summary: got %#v, want %#v", summaries[0], mockReview.Summary)
	}
	if !summaryEq(summaries[1], mockReview2.Summary) {
		t.Errorf("wrong review summary: got %#v, want %#v", summaries[1], mockReview.Summary)
	}

	newSum := mockReview2.Summary

	newSum.Submitter = "boris"

	db.SetSummary(2, newSum)

	summaries, err = db.GetSummaries()
	if err != nil {
		t.Fatal(err)
	}

	if !summaryEq(summaries[1], newSum) {
		t.Errorf("wrong review summary: got %#v, want %#v", summaries[1], newSum)
	}

	anno := review.Annotation{
		File:    0,
		Hunk:    0,
		Line:    0,
		Comment: "Fitter, happier, more productive",
		User:    "******",
	}
	gotReview.Revisions[0].Annotate(anno)

	if err := db.UpdateRevision(2, 0, gotReview.Revisions[0]); err != nil {
		t.Fatal(err)
	}

	mr2, err := db.GetReview(2)
	if err != nil {
		t.Fatal(err)
	}
	if annotations := mr2.Revisions[0].Annotations; len(annotations) != 2 {
		t.Errorf("wrong number of annotations: got %d, want %d", len(annotations), 2)
	} else if annotations[1] != anno {
		t.Errorf("wrong annotation: got %#v, want %#v", annotations[0], anno)
	}

	if err := db.DeleteReview(3); err == nil {
		t.Error("expected error")
	} else if _, ok := err.(ErrNoReview); !ok {
		t.Error("expected ErrNoReview")
	}

	if err := db.DeleteReview(1); err != nil {
		t.Fatal(err)
	}

	if summaries, err := db.GetSummaries(); err != nil {
		t.Fatal(err)
	} else if len(summaries) != 1 {
		t.Error("delete failed")
	}
}