package dehub import ( "reflect" "strings" "testing" "github.com/davecgh/go-spew/spew" "gopkg.in/src-d/go-git.v4/plumbing" ) func TestChangeCommitVerify(t *testing.T) { type step struct { msg string msgHead string // defaults to msg tree map[string]string } testCases := []struct { descr string steps []step }{ { descr: "single commit", steps: []step{ { msg: "first commit", tree: map[string]string{"a": "0", "b": "1"}, }, }, }, { descr: "multiple commits", steps: []step{ { msg: "first commit", tree: map[string]string{"a": "0", "b": "1"}, }, { msg: "second commit, changing a", tree: map[string]string{"a": "1"}, }, { msg: "third commit, empty", }, { msg: "fourth commit, adding c, removing b", tree: map[string]string{"b": "", "c": "2"}, }, }, }, { descr: "big body commits", steps: []step{ { msg: "first commit, single line but with newline\n", }, { msg: "second commit, single line but with two newlines\n\n", msgHead: "second commit, single line but with two newlines\n\n", }, { msg: "third commit, multi-line with one newline\nanother line!", msgHead: "third commit, multi-line with one newline\n\n", }, { msg: "fourth commit, multi-line with two newlines\n\nanother line!", msgHead: "fourth commit, multi-line with two newlines\n\n", }, }, }, } for _, test := range testCases { t.Run(test.descr, func(t *testing.T) { h := newHarness(t) rootSig := h.stageNewAccount("root", false) for _, step := range test.steps { h.stage(step.tree) gitCommit := h.assertCommitChange(true, step.msg, rootSig) if step.msgHead == "" { step.msgHead = strings.TrimSpace(step.msg) + "\n\n" } if !strings.HasPrefix(gitCommit.GitCommit.Message, step.msgHead) { t.Fatalf("commit message %q does not start with expected head %q", gitCommit.GitCommit.Message, step.msgHead) } var actualCommit Commit if err := actualCommit.UnmarshalText([]byte(gitCommit.GitCommit.Message)); err != nil { t.Fatalf("error unmarshaling commit body: %v", err) } else if !reflect.DeepEqual(actualCommit, gitCommit.Commit) { t.Fatalf("returned change commit:\n%s\ndoes not match actual one:\n%s", spew.Sdump(gitCommit.Commit), spew.Sdump(actualCommit)) } } }) } } func TestCombineCommitChanges(t *testing.T) { h := newHarness(t) // commit initial config, so the root user can modify it in the next commit rootSig := h.stageNewAccount("root", false) h.assertCommitChange(true, "initial commit", rootSig) // add a toot user and modify the access controls such that both accounts // are required for the main branch tootSig := h.stageNewAccount("toot", false) h.stageAccessControls(` - action: allow filters: - type: branch pattern: main - type: commit_type commit_type: change - type: signature any_account: true count: 2 - action: allow filters: - type: not filter: type: branch pattern: main - type: signature any_account: true count: 1 `) tootCommit := h.assertCommitChange(true, "add toot", rootSig) // make a single change commit in another branch using root. Then add a // credential using toot, and combine them onto main. otherBranch := plumbing.NewBranchReferenceName("other") h.checkout(otherBranch) h.stage(map[string]string{"foo": "bar"}) fooCommit := h.assertCommitChange(true, "add foo file", rootSig) // now adding a credential commit from toot should work credCommitObj, err := h.repo.NewCommitCredential(fooCommit.Interface.StoredHash()) if err != nil { t.Fatal(err) } credCommit := h.tryCommit(true, credCommitObj, tootSig) allCommits, err := h.repo.GetGitCommitRange( tootCommit.GitCommit.Hash, credCommit.GitCommit.Hash, ) if err != nil { t.Fatalf("error getting commits: %v", err) } combinedCommit, err := h.repo.CombineCommitChanges(allCommits, MainRefName) if err != nil { t.Fatal(err) } // that new commit should have both credentials creds := combinedCommit.Commit.Common.Credentials if len(creds) != 2 { t.Fatalf("combined commit has %d credentials, not 2", len(creds)) } else if creds[0].AccountID != "root" { t.Fatalf("combined commit first credential should be from root, is from %q", creds[0].AccountID) } else if creds[1].AccountID != "toot" { t.Fatalf("combined commit second credential should be from toot, is from %q", creds[1].AccountID) } // double check that the HEAD commit of main got properly set h.checkout(MainRefName) mainHead, err := h.repo.GetGitHead() if err != nil { t.Fatal(err) } else if mainHead.GitCommit.Hash != combinedCommit.GitCommit.Hash { t.Fatalf("mainHead's should be pointed at %s but is pointed at %s", combinedCommit.GitCommit.Hash, mainHead.GitCommit.Hash) } else if err = h.repo.VerifyCommits(MainRefName, []GitCommit{combinedCommit}); err != nil { t.Fatalf("unable to verify combined commit: %v", err) } else if author := combinedCommit.GitCommit.Author.Name; author != "root" { t.Fatalf("unexpected author value %q", author) } }