package main import ( "errors" "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" mock_main "src.opensuse.org/workflow-pr/mock" ) func TestMaintainership(t *testing.T) { allocateMaintainershipInterface := func(t *testing.T) (*mock_common.MockGiteaMaintainershipInterface, *mock_main.MockGiteaPRInterface) { ctl := gomock.NewController(t) mi := mock_common.NewMockGiteaMaintainershipInterface(ctl) pri := mock_main.NewMockGiteaPRInterface(ctl) return mi, pri } config := common.AutogitConfig{ Branch: "bar", Organization: "foo", GitProjectName: common.DefaultGitPrj, } packageTests := []struct { name string }{ { name: "No maintainer in empty package", }, } for _, test := range packageTests { t.Run(test.name, func(t *testing.T) { mi, _ := allocateMaintainershipInterface(t) mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, nil) maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch) if err != nil { t.Error("Invalid number of maintainers for package", err) } m := MaintainerListForPackage(maintainers, "bar") if len(m) != 0 { t.Error("Invalid number of maintainers for package", err) } }) } projectTests := []struct { name string maintainersFile []byte }{ { name: "No maintainer for empty project", }, { name: "No maintainer for empty project maintainer file", maintainersFile: []byte("{}"), }, } for _, test := range projectTests { t.Run(test.name, func(t *testing.T) { mi, _ := allocateMaintainershipInterface(t) mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(test.maintainersFile, nil) maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch) if err != nil { t.Error("Invalid number of maintainers for package", err) } m := MaintainerListForProject(maintainers) if len(m) != 0 { t.Error("Invalid number of maintainers for project", err) } }) } t.Run("Error in MaintainerListForPackage when remote has an error", func(t *testing.T) { mi, _ := allocateMaintainershipInterface(t) err := errors.New("some error here") mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, err) _, errRet := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch) switch errRet { case nil: t.Error("Should have returned an error") case err: break default: t.Error("Unexpected error received", err) } }) t.Run("Error in MaintainerListForProject when remote has an error", func(t *testing.T) { mi, _ := allocateMaintainershipInterface(t) err := errors.New("some error here") mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return(nil, err) _, errRet := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch) switch errRet { case nil: t.Error("Should have returned an error") case err: break default: t.Error("Unexpected error received", err) } }) t.Run("Multiple project maintainers", func(t *testing.T) { mi, _ := allocateMaintainershipInterface(t) mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(` { "": ["user1", "user2"] } `), nil) maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch) if err != nil { t.Error("Invalid number of maintainers for project", err) } m := MaintainerListForProject(maintainers) if len(m) != 2 { t.Error("Invalid number of maintainers for project", err) } if m[0] != "user1" || m[1] != "user2" { t.Error("Can't find expected users. Found", m) } }) t.Run("Single project maintainer", func(t *testing.T) { mi, _ := allocateMaintainershipInterface(t) mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(` { "": ["user"] } `), nil) maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch) if err != nil { t.Error("Invalid number of maintainers for project", err) } m := MaintainerListForProject(maintainers) if len(m) != 1 { t.Error("Invalid number of maintainers for project", err) } if m[0] != "user" { t.Error("Can't find expected users. Found", m) } }) t.Run("Invalid list of project maintainers", func(t *testing.T) { mi, _ := allocateMaintainershipInterface(t) mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(` { "": ["user", 4] } `), nil) _, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch) if err == nil { t.Error("Invalid number of maintainers for project", err) } }) t.Run("Invalid list of project maintainers", func(t *testing.T) { mi, _ := allocateMaintainershipInterface(t) mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(` { "": 4 } `), nil) _, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch) if err == nil { t.Error("Invalid number of maintainers for project", err) } }) t.Run("Multiple package maintainers", func(t *testing.T) { mi, _ := allocateMaintainershipInterface(t) mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(` { "pkg": ["user1", "user2"], "": ["user1", "user3"] } `), nil) maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch) if err != nil { t.Error("Invalid number of maintainers for package") } m := MaintainerListForPackage(maintainers, "pkg") if len(m) != 3 { t.Error("Invalid number of maintainers for package", m) } if m[0] != "user1" || m[1] != "user2" || m[2] != "user3" { t.Error("Can't find expected users. Found", m) } }) t.Run("No package maintainers and only project maintainer", func(t *testing.T) { mi, _ := allocateMaintainershipInterface(t) mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(` { "pkg": ["user1", "user2"], "": ["user1", "user3"] } `), nil) maintainers, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch) if err != nil { t.Error("Invalid number of maintainers for package", maintainers) } m := MaintainerListForPackage(maintainers, "bar") if len(m) != 2 { t.Error("Invalid number of maintainers for package", m) } if m[0] != "user1" || m[1] != "user3" { t.Error("Can't find expected users. Found", m) } }) t.Run("Invalid list of package maintainers", func(t *testing.T) { mi, _ := allocateMaintainershipInterface(t) mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(` { "pkg": 3, "": ["user", 4] } `), nil) _, err := ProjectMaintainershipData(mi, config.Organization, config.GitProjectName, config.Branch) if err == nil { t.Error("Invalid number of maintainers for project", err) } }) } func TestReviewApproval(t *testing.T) { allocateMaintainershipInterface := func(t *testing.T) (*mock_common.MockGiteaMaintainershipInterface, *mock_main.MockGiteaPRInterface) { ctl := gomock.NewController(t) mi := mock_common.NewMockGiteaMaintainershipInterface(ctl) pri := mock_main.NewMockGiteaPRInterface(ctl) return mi, pri } config := common.AutogitConfig{ Branch: "bar", Organization: "foo", GitProjectName: common.DefaultGitPrj, } t.Run("Maintainers not appoved", func(t *testing.T) { mi, pri := allocateMaintainershipInterface(t) pri.EXPECT().GetPullRequestAndReviews("foo", common.DefaultGitPrj, int64(10)).Return( &models.PullRequest{ Body: "", Index: 10, RequestedReviewers: []*models.User{}, }, []*models.PullReview{}, nil, ) mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(`{"foo": ["bingo"]}`), nil) approved, err := IsPrjGitPRApproved(mi, pri, config, 10) if approved || err != nil { t.Error("Unexpected approved or err:", err) } }) t.Run("Maintainers approved", func(t *testing.T) { mi, pri := allocateMaintainershipInterface(t) pri.EXPECT().GetPullRequestAndReviews("foo", common.DefaultGitPrj, int64(10)).Return( &models.PullRequest{ Body: "", Index: 10, RequestedReviewers: []*models.User{}, }, []*models.PullReview{{Body: "ok", User: &models.User{UserName: "test"}, Stale: false, State: "approved"}}, nil, ) mi.EXPECT().FetchMaintainershipFile("foo", common.DefaultGitPrj, "bar").Return([]byte(` "": ["test"] `), nil) approved, err := IsPrjGitPRApproved(mi, pri, config, 10) if !approved || err != nil { t.Error("Unexpected disapproval or err:", err) } }) }