File view with blame information shown in the left gutter beside each line.

barerepo / server / e2e/copy_test.go
111 lines · 4.1kb · master
log files threads runs releases config jump to file t
133728e barerepo 1mo
1
package e2e
133728e barerepo 1mo
2
133728e barerepo 1mo
3
import (
133728e barerepo 1mo
4
"context"
133728e barerepo 1mo
5
"net/http"
133728e barerepo 1mo
6
"net/url"
133728e barerepo 1mo
7
"os/exec"
133728e barerepo 1mo
8
"strings"
133728e barerepo 1mo
9
"testing"
133728e barerepo 1mo
10
)
133728e barerepo 1mo
11
133728e barerepo 1mo
12
// post sends a form as one account, without following the redirect, so the answer can be read.
133728e barerepo 1mo
13
func post(t *testing.T, in *instance, account, path string, form url.Values) *http.Response {
133728e barerepo 1mo
14
t.Helper()
133728e barerepo 1mo
15
body := strings.NewReader(form.Encode())
133728e barerepo 1mo
16
req, err := http.NewRequest(http.MethodPost, in.http.URL+path, body)
133728e barerepo 1mo
17
if err != nil {
133728e barerepo 1mo
18
t.Fatal(err)
133728e barerepo 1mo
19
}
133728e barerepo 1mo
20
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
133728e barerepo 1mo
21
if account != "" {
133728e barerepo 1mo
22
token, err := in.db.NewSession(context.Background(), account)
133728e barerepo 1mo
23
if err != nil {
133728e barerepo 1mo
24
t.Fatal(err)
133728e barerepo 1mo
25
}
133728e barerepo 1mo
26
req.AddCookie(&http.Cookie{Name: "barerepo_session", Value: token})
133728e barerepo 1mo
27
}
133728e barerepo 1mo
28
client := &http.Client{CheckRedirect: func(*http.Request, []*http.Request) error {
133728e barerepo 1mo
29
return http.ErrUseLastResponse
133728e barerepo 1mo
30
}}
133728e barerepo 1mo
31
resp, err := client.Do(req)
133728e barerepo 1mo
32
if err != nil {
133728e barerepo 1mo
33
t.Fatal(err)
133728e barerepo 1mo
34
}
133728e barerepo 1mo
35
resp.Body.Close()
133728e barerepo 1mo
36
return resp
133728e barerepo 1mo
37
}
133728e barerepo 1mo
38
133728e barerepo 1mo
39
// Chapter 21.1. Copying is the answer to "no forks", so a reader who is not the owner performs it.
133728e barerepo 1mo
40
func TestAReaderCopiesARepositoryFromTheConfigPage(t *testing.T) {
133728e barerepo 1mo
41
if _, err := exec.LookPath("git"); err != nil {
133728e barerepo 1mo
42
t.Skip("git is not installed")
133728e barerepo 1mo
43
}
133728e barerepo 1mo
44
in := newInstance(t)
133728e barerepo 1mo
45
john := in.account("john")
133728e barerepo 1mo
46
in.account("lisa")
133728e barerepo 1mo
47
seed(t, in, john, "john", "johnbot")
133728e barerepo 1mo
48
133728e barerepo 1mo
49
// A proposal ref exists on the original, and chapter 21.1 says it must not come across.
133728e barerepo 1mo
50
work := clone(t, in, john, "/john/johnbot")
133728e barerepo 1mo
51
commit(t, work, "package main\n\nfunc main() {}\n", "a proposal")
133728e barerepo 1mo
52
run(t, work, "git", "push", "-q", in.url(john, "/john/johnbot"), "HEAD:refs/proposals/new")
133728e barerepo 1mo
53
133728e barerepo 1mo
54
// Guard the guard: if the original has no proposal ref, the assertion below proves nothing.
133728e barerepo 1mo
55
if refs := run(t, repoDir(t, in, "john", "johnbot"), "git", "for-each-ref",
133728e barerepo 1mo
56
"--format=%(refname)"); !strings.Contains(refs, "refs/proposals/") {
133728e barerepo 1mo
57
t.Fatalf("the push made no proposal ref, so this test cannot show one is left behind:\n%s", refs)
133728e barerepo 1mo
58
}
133728e barerepo 1mo
59
133728e barerepo 1mo
60
resp := post(t, in, "lisa", "/john/johnbot/copy", nil)
133728e barerepo 1mo
61
if resp.StatusCode != http.StatusFound {
133728e barerepo 1mo
62
t.Fatalf("the copy answered %d, wanted a redirect to the new repository", resp.StatusCode)
133728e barerepo 1mo
63
}
133728e barerepo 1mo
64
if got := resp.Header.Get("Location"); got != "/lisa/johnbot" {
133728e barerepo 1mo
65
t.Errorf("the copy sent the reader to %q", got)
133728e barerepo 1mo
66
}
133728e barerepo 1mo
67
133728e barerepo 1mo
68
if code, _, _ := get(t, in.http.URL+"/lisa/johnbot"); code != http.StatusOK {
133728e barerepo 1mo
69
t.Fatalf("the copy is not being served: %d", code)
133728e barerepo 1mo
70
}
133728e barerepo 1mo
71
133728e barerepo 1mo
72
dir := repoDir(t, in, "lisa", "johnbot")
133728e barerepo 1mo
73
refs := run(t, dir, "git", "for-each-ref", "--format=%(refname)")
133728e barerepo 1mo
74
if !strings.Contains(refs, "refs/heads/master") {
133728e barerepo 1mo
75
t.Errorf("the copy has no branches:\n%s", refs)
133728e barerepo 1mo
76
}
133728e barerepo 1mo
77
if strings.Contains(refs, "refs/proposals/") {
133728e barerepo 1mo
78
t.Errorf("proposal refs came across, and chapter 21.1 says they belong to the original:\n%s", refs)
133728e barerepo 1mo
79
}
133728e barerepo 1mo
80
// Chapter 21.1 copies threads and notes, which is the half a fast object copy does not bring.
133728e barerepo 1mo
81
if !strings.Contains(refs, "refs/notes/threads/") {
133728e barerepo 1mo
82
t.Errorf("the discussion did not come across, so the copy lost the conversation:\n%s", refs)
133728e barerepo 1mo
83
}
133728e barerepo 1mo
84
if _, _, page := get(t, in.http.URL+"/lisa/johnbot/threads"); !strings.Contains(page, "1") {
133728e barerepo 1mo
85
t.Errorf("the copy's thread list is empty:\n%s", page)
133728e barerepo 1mo
86
}
133728e barerepo 1mo
87
133728e barerepo 1mo
88
// A copy borrows the original's objects, so deleting the original must give them to the copy first.
133728e barerepo 1mo
89
post(t, in, "john", "/john/johnbot/delete", url.Values{"confirm": {"johnbot"}})
133728e barerepo 1mo
90
if code, _, _ := get(t, in.http.URL+"/lisa/johnbot"); code != http.StatusOK {
133728e barerepo 1mo
91
t.Fatalf("deleting the original took the copy's history with it: %d", code)
133728e barerepo 1mo
92
}
133728e barerepo 1mo
93
if out := run(t, dir, "git", "log", "--format=%s", "master"); !strings.Contains(out, "first") {
133728e barerepo 1mo
94
t.Errorf("the copy lost its history when the original went:\n%s", out)
133728e barerepo 1mo
95
}
133728e barerepo 1mo
96
}
133728e barerepo 1mo
97
133728e barerepo 1mo
98
// A signed-out reader is sent to sign in, because a copy needs an account to own it.
133728e barerepo 1mo
99
func TestCopyNeedsAnAccount(t *testing.T) {
133728e barerepo 1mo
100
if _, err := exec.LookPath("git"); err != nil {
133728e barerepo 1mo
101
t.Skip("git is not installed")
133728e barerepo 1mo
102
}
133728e barerepo 1mo
103
in := newInstance(t)
133728e barerepo 1mo
104
john := in.account("john")
133728e barerepo 1mo
105
seed(t, in, john, "john", "johnbot")
133728e barerepo 1mo
106
133728e barerepo 1mo
107
resp := post(t, in, "", "/john/johnbot/copy", nil)
133728e barerepo 1mo
108
if got := resp.Header.Get("Location"); got != "/signin" {
133728e barerepo 1mo
109
t.Errorf("an anonymous copy went to %q, wanted /signin", got)
133728e barerepo 1mo
110
}
133728e barerepo 1mo
111
}
history · rawbarerepo 0.1.0