| 1 | package server |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "net/http/httptest" |
| 6 | "path/filepath" |
| 7 | "strconv" |
| 8 | "strings" |
| 9 | "sync" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "gitlab.com/Timfa/bluegit/internal/auth" |
| 14 | "gitlab.com/Timfa/bluegit/internal/config" |
| 15 | "gitlab.com/Timfa/bluegit/internal/store" |
| 16 | "gitlab.com/Timfa/bluegit/internal/webhook" |
| 17 | ) |
| 18 | |
| 19 | type boxMail struct { |
| 20 | mu sync.Mutex |
| 21 | n int |
| 22 | user, subject, html string |
| 23 | wait chan struct{} |
| 24 | } |
| 25 | |
| 26 | func (b *boxMail) SendEmail(ctx context.Context, username, subject, html string) error { |
| 27 | b.mu.Lock() |
| 28 | b.n++ |
| 29 | b.user, b.subject, b.html = username, subject, html |
| 30 | b.mu.Unlock() |
| 31 | if b.wait != nil { |
| 32 | b.wait <- struct{}{} |
| 33 | } |
| 34 | return nil |
| 35 | } |
| 36 | |
| 37 | func TestFailureMailUsesRecipientLanguage(t *testing.T) { |
| 38 | storage := t.TempDir() |
| 39 | st, err := store.Open(filepath.Join(storage, "bluegit.db"), bytes32()) |
| 40 | if err != nil { |
| 41 | t.Fatal(err) |
| 42 | } |
| 43 | t.Cleanup(func() { _ = st.Close() }) |
| 44 | cfg := config.Config{ |
| 45 | BaseURL: "http://127.0.0.1:9", Listen: "127.0.0.1:0", StoragePath: storage, |
| 46 | Auth: config.Auth{Mode: "dev", DevUser: "tim", DevAuthID: 1, UserAgent: "test"}, |
| 47 | Git: config.Git{Binary: "git", DefaultBranch: "master"}, |
| 48 | } |
| 49 | wait := make(chan struct{}, 1) |
| 50 | box := &boxMail{wait: wait} |
| 51 | srv := &Server{Cfg: cfg, Store: st, Auth: auth.Dev{Username: "tim", AuthID: 1}, Dev: true, Git: "git", Mail: box} |
| 52 | ts := httptest.NewServer(srv.Handler()) |
| 53 | t.Cleanup(ts.Close) |
| 54 | res, err := ts.Client().Get(ts.URL + "/login") |
| 55 | if err != nil { |
| 56 | t.Fatal(err) |
| 57 | } |
| 58 | _ = res.Body.Close() |
| 59 | |
| 60 | ctx := context.Background() |
| 61 | user, err := st.UserByUsername(ctx, "tim") |
| 62 | if err != nil { |
| 63 | t.Fatal(err) |
| 64 | } |
| 65 | if err := st.SetLanguage(ctx, user.ID, "nl"); err != nil { |
| 66 | t.Fatal(err) |
| 67 | } |
| 68 | repo, err := st.CreateRepo(ctx, "user", user.ID, "mail", "", "master", "private") |
| 69 | if err != nil { |
| 70 | t.Fatal(err) |
| 71 | } |
| 72 | id, err := st.CreatePipeline(ctx, repo.ID, "abc", "refs/tags/v0.0.26", "push", "tim", "tag die faalt", []store.NewJob{ |
| 73 | {Name: "hardjob", Stage: "test", Script: "exit 1"}, |
| 74 | {Name: "softjob", Stage: "test", Script: "exit 1", AllowFailure: true}, |
| 75 | }) |
| 76 | if err != nil { |
| 77 | t.Fatal(err) |
| 78 | } |
| 79 | jobs, _ := st.JobsByPipeline(ctx, id) |
| 80 | for _, j := range jobs { |
| 81 | if err := st.FinishJob(ctx, j.ID, "failed", ""); err != nil { |
| 82 | t.Fatal(err) |
| 83 | } |
| 84 | } |
| 85 | srv.Dispatch(repo.ID, webhook.Event{Type: webhook.EventPipelineSuccess, PipelineID: strconv.FormatInt(id, 10)}) |
| 86 | if box.n != 0 { |
| 87 | t.Fatalf("succes mailde %d", box.n) |
| 88 | } |
| 89 | srv.Dispatch(repo.ID, webhook.Event{Type: webhook.EventPipelineFailure, PipelineID: strconv.FormatInt(id, 10)}) |
| 90 | select { |
| 91 | case <-wait: |
| 92 | case <-time.After(2 * time.Second): |
| 93 | t.Fatal("geen mail") |
| 94 | } |
| 95 | box.mu.Lock() |
| 96 | defer box.mu.Unlock() |
| 97 | if box.user != "tim" || !strings.Contains(box.subject, "mislukt") || !strings.Contains(box.subject, "tim/mail") { |
| 98 | t.Fatalf("onderwerp %s voor %s", box.subject, box.user) |
| 99 | } |
| 100 | for _, want := range []string{"v0.0.26", "hardjob", "Mislukte jobs", "/-/pipelines/" + strconv.FormatInt(id, 10), "/jobs/"} { |
| 101 | if !strings.Contains(box.html, want) { |
| 102 | t.Errorf("html mist %q\n%s", want, box.html) |
| 103 | } |
| 104 | } |
| 105 | if strings.Contains(box.html, "softjob") { |
| 106 | t.Fatalf("allow_failure hoort niet in de mail:\n%s", box.html) |
| 107 | } |
| 108 | } |