package common_test import ( "slices" "testing" "go.uber.org/mock/gomock" "src.opensuse.org/autogits/common" "src.opensuse.org/autogits/common/gitea-generated/models" mock_common "src.opensuse.org/autogits/common/mock" ) func TestLabelKey(t *testing.T) { tests := map[string]string{ "": "", "foo": "Foo", "foo/bar": "Foobar", "foo/Bar": "FooBar", } for k, v := range tests { if c := common.LabelKey(k); c != v { t.Error("expected", v, "got", c, "input", k) } } } func TestConfigLabelParser(t *testing.T) { tests := []struct { name string json string label_value string }{ { name: "empty", json: "{}", label_value: "path/String", }, { name: "defined", json: `{"Labels": {"foo": "bar", "PathString": "moo/Label"}}`, label_value: "moo/Label", }, { name: "undefined", json: `{"Labels": {"foo": "bar", "NotPathString": "moo/Label"}}`, label_value: "path/String", }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { repo := models.Repository{ DefaultBranch: "master", } ctl := gomock.NewController(t) gitea := mock_common.NewMockGiteaFileContentAndRepoFetcher(ctl) gitea.EXPECT().GetRepositoryFileContent("foo", "bar", "", "workflow.config").Return([]byte(test.json), "abc", nil) gitea.EXPECT().GetRepository("foo", "bar").Return(&repo, nil) config, err := common.ReadWorkflowConfig(gitea, "foo/bar") if err != nil || config == nil { t.Fatal(err) } if l := config.Label("path/String"); l != test.label_value { t.Error("Expecting", test.label_value, "got", l) } }) } } func TestProjectConfigMatcher(t *testing.T) { configs := common.AutogitConfigs{ { Organization: "test", GitProjectName: "test/prjgit#main", }, { Organization: "test", Branch: "main", GitProjectName: "test/prjgit#main", }, { Organization: "test", Branch: "main", GitProjectName: "test/bar#never_match", }, { Organization: "test", GitProjectName: "test/bar#main", }, } tests := []struct { name string org string repo string branch string config int }{ { name: "invalid match", org: "foo", repo: "bar", config: -1, }, { name: "default branch", org: "test", repo: "foo", branch: "", config: 0, }, { name: "main branch", org: "test", repo: "foo", branch: "main", config: 1, }, { name: "prjgit only match", org: "test", repo: "bar", branch: "main", config: 3, }, { name: "non-default branch match", org: "test", repo: "bar", branch: "something_main", config: -1, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { c := configs.GetPrjGitConfig(test.org, test.repo, test.branch) if test.config < 0 { if c != nil { t.Fatal("Expected nil. Got:", *c) } } else if config := configs[test.config]; c != config { t.Fatal("Expected", *config, "got", *c) } }) } } func TestConfigWorkflowParser(t *testing.T) { tests := []struct { name string config_json string repo models.Repository }{ { name: "Regular workflow file", config_json: `{ "Workflows": ["direct", "pr"], "Organization": "testing", "ReviewGroups": [ { "Name": "gnuman1", "Reviewers": ["adamm"] } ] }`, repo: models.Repository{ DefaultBranch: "master", }, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { ctl := gomock.NewController(t) gitea := mock_common.NewMockGiteaFileContentAndRepoFetcher(ctl) gitea.EXPECT().GetRepositoryFileContent("foo", "bar", "", "workflow.config").Return([]byte(test.config_json), "abc", nil) gitea.EXPECT().GetRepository("foo", "bar").Return(&test.repo, nil) config, err := common.ReadWorkflowConfig(gitea, "foo/bar") if err != nil { t.Fatal(err) } if config.ManualMergeOnly != false { t.Fatal("This should be false") } if config.Label("foobar") != "foobar" { t.Fatal("undefined label should return default value") } }) } } // FIXME: should test ReadWorkflowConfig as it will always set prjgit completely func TestProjectGitParser(t *testing.T) { tests := []struct { name string prjgit string org string branch string res [3]string }{ { name: "repo only", prjgit: "repo.git#master", org: "org", branch: "br", res: [3]string{"org", "repo.git", "master"}, }, { name: "default", org: "org", prjgit: "org/_ObsPrj#master", res: [3]string{"org", common.DefaultGitPrj, "master"}, }, { name: "repo with branch", org: "org2", prjgit: "org2/repo.git#somebranch", res: [3]string{"org2", "repo.git", "somebranch"}, }, { name: "repo org and branch", org: "org3", prjgit: "oorg/foo.bar#point", res: [3]string{"oorg", "foo.bar", "point"}, }, { name: "whitespace shouldn't matter", prjgit: " oorg / \nfoo.bar\t # point ", res: [3]string{"oorg", "foo.bar", "point"}, }, { name: "repo org and empty branch", org: "org3", prjgit: "oorg/foo.bar#master", res: [3]string{"oorg", "foo.bar", "master"}, }, { name: "only branch defined", org: "org3", prjgit: "org3/_ObsPrj#mybranch", res: [3]string{"org3", "_ObsPrj", "mybranch"}, }, { name: "only org and branch defined", org: "org3", prjgit: "org1/_ObsPrj#mybranch", res: [3]string{"org1", "_ObsPrj", "mybranch"}, }, { name: "empty org and repo", org: "org3", prjgit: "org3/repo#master", res: [3]string{"org3", "repo", "master"}, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { c := &common.AutogitConfig{ Organization: test.org, Branch: test.branch, GitProjectName: test.prjgit, } i, j, k := c.GetPrjGit() res := []string{i, j, k} if !slices.Equal(res, test.res[:]) { t.Error("Expected", test.res, "but received", res) } }) } } func TestConfigPermissions(t *testing.T) { tests := []struct { name string permission string user string config *common.AutogitConfig result bool }{ { name: "NoPermissions", permission: common.Permission_ForceMerge, }, { name: "NoPermissions", permission: common.Permission_Group, }, { name: "Regular permission ForcePush", permission: common.Permission_ForceMerge, result: true, user: "user", config: &common.AutogitConfig{ Permissions: []*common.Permissions{ &common.Permissions{ Permission: common.Permission_ForceMerge, Members: []string{"user"}, }, }, }, }, { name: "User is part of a group", permission: common.Permission_ForceMerge, result: true, user: "user", config: &common.AutogitConfig{ Permissions: []*common.Permissions{ &common.Permissions{ Permission: common.Permission_ForceMerge, Members: []string{"group"}, }, }, ReviewGroups: []*common.ReviewGroup{ &common.ReviewGroup{ Name: "group", Reviewers: []string{"some", "members", "including", "user"}, }, }, }, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { if r := test.config.HasPermission(test.user, test.permission); r != test.result { t.Error("Expecting", test.result, "but got opposite") } if r := test.config.HasPermission(test.user+test.user, test.permission); r { t.Error("Expecting false for fake user, but got opposite") } }) } }