Repository navigation
Live Account lens rule, lens route, import tool create route and retry #5336
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Copyright The Linux Foundation and each contributor to CommunityBridge. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package cla_manager | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
|
|
||
| "github.com/go-openapi/runtime/middleware" | ||
| "github.com/linuxfoundation/easycla/cla-backend-go/company" | ||
| "github.com/linuxfoundation/easycla/cla-backend-go/gen/v1/models" | ||
| "github.com/linuxfoundation/easycla/cla-backend-go/gen/v1/restapi/operations" | ||
| "github.com/linuxfoundation/easycla/cla-backend-go/gen/v1/restapi/operations/cla_manager" | ||
| log "github.com/linuxfoundation/easycla/cla-backend-go/logging" | ||
| service2 "github.com/linuxfoundation/easycla/cla-backend-go/project/service" | ||
| "github.com/linuxfoundation/easycla/cla-backend-go/utils" | ||
| "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| func companyCLAGroupOrgLens(ctx context.Context, companyService company.IService, projectService service2.Service, lensURL func(ctx context.Context, companySFID, claGroupID string) string, companyID, claGroupID string) (*models.CompanyClaGroupOrgLens, error) { | ||
| companyModel, err := companyService.GetCompany(ctx, companyID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if companyModel == nil { | ||
| return nil, &utils.CompanyNotFound{CompanyID: companyID} | ||
| } | ||
| claGroupModel, err := projectService.GetCLAGroupByID(ctx, claGroupID) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| if claGroupModel == nil { | ||
| return nil, &utils.CLAGroupNotFound{CLAGroupID: claGroupID} | ||
| } | ||
| return &models.CompanyClaGroupOrgLens{ | ||
| CompanyID: companyModel.CompanyID, | ||
| CompanySFID: companyModel.CompanyExternalID, | ||
| ClaGroupID: claGroupModel.ProjectID, | ||
| FoundationSFID: claGroupModel.FoundationSFID, | ||
| OrgLensURL: lensURL(ctx, companyModel.CompanyExternalID, claGroupModel.ProjectID), | ||
| }, nil | ||
| } | ||
|
|
||
| func configureOrgLens(api *operations.ClaAPI, companyService company.IService, projectService service2.Service, lensURL func(ctx context.Context, companySFID, claGroupID string) string) { | ||
| api.ClaManagerGetCompanyCLAGroupOrgLensHandler = cla_manager.GetCompanyCLAGroupOrgLensHandlerFunc(func(params cla_manager.GetCompanyCLAGroupOrgLensParams) middleware.Responder { | ||
| reqID := utils.GetRequestID(params.XREQUESTID) | ||
| ctx := context.WithValue(params.HTTPRequest.Context(), utils.XREQUESTID, reqID) // nolint | ||
| lens, err := companyCLAGroupOrgLens(ctx, companyService, projectService, lensURL, params.CompanyID, params.ClaGroupID) | ||
| if err != nil { | ||
| var companyNotFound *utils.CompanyNotFound | ||
| var claGroupNotFound *utils.CLAGroupNotFound | ||
| if errors.As(err, &companyNotFound) || errors.As(err, &claGroupNotFound) { | ||
| return cla_manager.NewGetCompanyCLAGroupOrgLensNotFound().WithXRequestID(reqID).WithPayload(&models.ErrorResponse{Code: "404", Message: err.Error()}) | ||
| } | ||
| log.WithFields(logrus.Fields{ | ||
| "functionName": "cla_manager.handlers.GetCompanyCLAGroupOrgLens", | ||
| utils.XREQUESTID: reqID, | ||
| "companyID": params.CompanyID, | ||
| "claGroupID": params.ClaGroupID, | ||
| }).WithError(err).Warn("unable to load the company or the CLA Group") | ||
| return cla_manager.NewGetCompanyCLAGroupOrgLensInternalServerError().WithXRequestID(reqID).WithPayload(&models.ErrorResponse{Code: "500", Message: "unable to load the company or the CLA Group"}) | ||
| } | ||
| return cla_manager.NewGetCompanyCLAGroupOrgLensOK().WithXRequestID(reqID).WithPayload(lens) | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // Copyright The Linux Foundation and each contributor to CommunityBridge. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package cla_manager | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/go-openapi/runtime" | ||
| "github.com/golang/mock/gomock" | ||
| mock_company "github.com/linuxfoundation/easycla/cla-backend-go/company/mocks" | ||
| "github.com/linuxfoundation/easycla/cla-backend-go/gen/v1/models" | ||
| "github.com/linuxfoundation/easycla/cla-backend-go/gen/v1/restapi/operations" | ||
| claManagerOps "github.com/linuxfoundation/easycla/cla-backend-go/gen/v1/restapi/operations/cla_manager" | ||
| mock_project "github.com/linuxfoundation/easycla/cla-backend-go/project/mocks" | ||
| "github.com/linuxfoundation/easycla/cla-backend-go/utils" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| const ( | ||
| orgLensTestBase = "https://app.example.org" | ||
| orgLensTestCompanyID = "00000000-0000-4000-8000-00000000000c" | ||
| orgLensTestSFID = "001000000000000AAA" | ||
| orgLensTestCLAGroupID = "00000000-0000-4000-8000-000000000001" | ||
| orgLensTestFoundation = "a0P000000000001AAA" | ||
| ) | ||
|
|
||
| func orgLensTestURL(ctx context.Context, companySFID, claGroupID string) string { | ||
| return utils.OrgLensURLContext(ctx, orgLensTestBase, companySFID, claGroupID) | ||
| } | ||
|
|
||
| func TestGetCompanyCLAGroupOrgLens(t *testing.T) { | ||
| company := &models.Company{CompanyID: orgLensTestCompanyID, CompanyExternalID: orgLensTestSFID, CompanyName: "Example Org"} | ||
| claGroup := &models.ClaGroup{ProjectID: orgLensTestCLAGroupID, FoundationSFID: orgLensTestFoundation, ProjectName: "Example Group"} | ||
| page := orgLensTestBase + "/org/" + orgLensTestSFID + "/easycla/" + orgLensTestCLAGroupID | ||
|
|
||
| tests := []struct { | ||
| name string | ||
| company *models.Company | ||
| companyErr error | ||
| claGroup *models.ClaGroup | ||
| groupErr error | ||
| live bool | ||
| wantStatus int | ||
| wantBody string | ||
| }{ | ||
| { | ||
| name: "live Account gets the lens page", company: company, claGroup: claGroup, live: true, wantStatus: http.StatusOK, | ||
| wantBody: `{"claGroupID":"` + orgLensTestCLAGroupID + `","companyID":"` + orgLensTestCompanyID + `","companySFID":"` + orgLensTestSFID + `","foundationSFID":"` + orgLensTestFoundation + `","orgLensURL":"` + page + `"}`, | ||
| }, | ||
| { | ||
| name: "Account that is not live keeps the Corporate Console", company: company, claGroup: claGroup, wantStatus: http.StatusOK, | ||
| wantBody: `{"claGroupID":"` + orgLensTestCLAGroupID + `","companyID":"` + orgLensTestCompanyID + `","companySFID":"` + orgLensTestSFID + `","foundationSFID":"` + orgLensTestFoundation + `","orgLensURL":""}`, | ||
| }, | ||
| { | ||
| name: "company without a Salesforce Account keeps the Corporate Console", | ||
| company: &models.Company{CompanyID: orgLensTestCompanyID, CompanyExternalID: "lf0000000000000AAA"}, claGroup: claGroup, live: true, wantStatus: http.StatusOK, | ||
| wantBody: `{"claGroupID":"` + orgLensTestCLAGroupID + `","companyID":"` + orgLensTestCompanyID + `","companySFID":"lf0000000000000AAA","foundationSFID":"` + orgLensTestFoundation + `","orgLensURL":""}`, | ||
| }, | ||
| { | ||
| name: "unknown company", companyErr: &utils.CompanyNotFound{CompanyID: orgLensTestCompanyID}, live: true, wantStatus: http.StatusNotFound, | ||
| wantBody: `{"Code":"404","Message":"company does not exist - company ID: ` + orgLensTestCompanyID + `"}`, | ||
| }, | ||
| { | ||
| name: "unknown CLA Group", company: company, groupErr: &utils.CLAGroupNotFound{CLAGroupID: orgLensTestCLAGroupID}, live: true, wantStatus: http.StatusNotFound, | ||
| wantBody: `{"Code":"404","Message":"cla group ` + orgLensTestCLAGroupID + ` not found"}`, | ||
| }, | ||
| { | ||
| name: "company lookup fails", companyErr: errors.New("dynamodb is down"), live: true, wantStatus: http.StatusInternalServerError, | ||
| wantBody: `{"Code":"500","Message":"unable to load the company or the CLA Group"}`, | ||
| }, | ||
| { | ||
| name: "CLA Group lookup fails", company: company, groupErr: errors.New("dynamodb is down"), live: true, wantStatus: http.StatusInternalServerError, | ||
| wantBody: `{"Code":"500","Message":"unable to load the company or the CLA Group"}`, | ||
| }, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| utils.SetOrgLensLiveCheck(func(context.Context, string) bool { return tt.live }) | ||
| t.Cleanup(func() { utils.SetOrgLensLiveCheck(nil) }) | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
| companyService := mock_company.NewMockIService(ctrl) | ||
| projectService := mock_project.NewMockService(ctrl) | ||
| companyService.EXPECT().GetCompany(gomock.Any(), orgLensTestCompanyID).Return(tt.company, tt.companyErr) | ||
| if tt.companyErr == nil { | ||
| projectService.EXPECT().GetCLAGroupByID(gomock.Any(), orgLensTestCLAGroupID).Return(tt.claGroup, tt.groupErr) | ||
| } | ||
|
|
||
| api := operations.NewClaAPI(nil) | ||
| configureOrgLens(api, companyService, projectService, orgLensTestURL) | ||
| reqID := "req-lens" | ||
| recorder := httptest.NewRecorder() | ||
| api.ClaManagerGetCompanyCLAGroupOrgLensHandler.Handle(claManagerOps.GetCompanyCLAGroupOrgLensParams{ | ||
| HTTPRequest: httptest.NewRequest(http.MethodGet, "/v3/company/"+orgLensTestCompanyID+"/cla-group/"+orgLensTestCLAGroupID+"/org-lens", nil), | ||
| XREQUESTID: &reqID, | ||
| CompanyID: orgLensTestCompanyID, | ||
| ClaGroupID: orgLensTestCLAGroupID, | ||
| }).WriteResponse(recorder, runtime.JSONProducer()) | ||
|
|
||
| assert.Equal(t, tt.wantStatus, recorder.Code, recorder.Body.String()) | ||
| assert.Equal(t, reqID, recorder.Header().Get("X-Request-Id")) | ||
| assert.JSONEq(t, tt.wantBody, recorder.Body.String()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestCompanyCLAGroupOrgLensWithoutARecord(t *testing.T) { | ||
| ctrl := gomock.NewController(t) | ||
| defer ctrl.Finish() | ||
| companyService := mock_company.NewMockIService(ctrl) | ||
| projectService := mock_project.NewMockService(ctrl) | ||
|
|
||
| companyService.EXPECT().GetCompany(gomock.Any(), orgLensTestCompanyID).Return(nil, nil) | ||
| _, err := companyCLAGroupOrgLens(context.Background(), companyService, projectService, orgLensTestURL, orgLensTestCompanyID, orgLensTestCLAGroupID) | ||
| var companyNotFound *utils.CompanyNotFound | ||
| require.ErrorAs(t, err, &companyNotFound) | ||
| assert.Equal(t, orgLensTestCompanyID, companyNotFound.CompanyID) | ||
|
|
||
| companyService.EXPECT().GetCompany(gomock.Any(), orgLensTestCompanyID).Return(&models.Company{CompanyID: orgLensTestCompanyID}, nil) | ||
| projectService.EXPECT().GetCLAGroupByID(gomock.Any(), orgLensTestCLAGroupID).Return(nil, nil) | ||
| _, err = companyCLAGroupOrgLens(context.Background(), companyService, projectService, orgLensTestURL, orgLensTestCompanyID, orgLensTestCLAGroupID) | ||
| var claGroupNotFound *utils.CLAGroupNotFound | ||
| require.ErrorAs(t, err, &claGroupNotFound) | ||
| assert.Equal(t, orgLensTestCLAGroupID, claGroupNotFound.CLAGroupID) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| // Copyright The Linux Foundation and each contributor to CommunityBridge. | ||
| // SPDX-License-Identifier: MIT | ||
|
|
||
| package cmd | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/http" | ||
| "net/http/httptest" | ||
| "testing" | ||
|
|
||
| "github.com/linuxfoundation/easycla/cla-backend-go/config" | ||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestOrgLensLiveCheckNeedsOnlyTheLensBase(t *testing.T) { | ||
| const sfid = "0014100000Te0G7AAJ" | ||
| lookups := 0 | ||
| server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
| body := "" | ||
| switch r.URL.Path { | ||
| case "/oauth/token": | ||
| body = `{"access_token":"member-token","token_type":"Bearer","expires_in":3600}` | ||
| case "/b2b_orgs/" + sfid: | ||
| lookups++ | ||
| body = `{"uid":"` + sfid + `","name":"Example Org"}` | ||
| default: | ||
| http.NotFound(w, r) | ||
| return | ||
| } | ||
| if _, err := w.Write([]byte(body)); err != nil { | ||
| t.Error(err) | ||
| } | ||
| })) | ||
| defer server.Close() | ||
| cfg := config.Config{ | ||
| OrgLensURL: "https://app.example.org", | ||
| MemberService: config.MemberService{BaseURL: server.URL, Audience: "member-audience"}, | ||
| Auth0Platform: config.Auth0Platform{URL: server.URL + "/oauth/token", ClientID: "test-client", ClientSecret: "test-secret"}, | ||
| } | ||
| members, err := newMemberServiceClient(cfg) | ||
| require.NoError(t, err) | ||
| require.Nil(t, members, "both member-service flags are off") | ||
|
|
||
| assert.True(t, newOrgLensLiveCheck(cfg, members)(context.Background(), sfid), "the lens base alone is enough for the live check") | ||
| assert.Equal(t, 1, lookups) | ||
|
|
||
| cfg.OrgLensURL = "" | ||
| assert.False(t, newOrgLensLiveCheck(cfg, members)(context.Background(), sfid), "without a lens base nothing asks member-service") | ||
| assert.Equal(t, 1, lookups) | ||
|
|
||
| cfg.OrgLensURL, cfg.MemberService.BaseURL = "https://app.example.org", "" | ||
| assert.False(t, newOrgLensLiveCheck(cfg, members)(context.Background(), sfid), "member-service not configured: the Corporate Console link stays") | ||
|
|
||
| cfg.MemberService.BaseURL = server.URL | ||
| cfg.MemberService.CreateCompanyEnabled = true | ||
| members, err = newMemberServiceClient(cfg) | ||
| require.NoError(t, err) | ||
| require.NotNil(t, members) | ||
| assert.True(t, newOrgLensLiveCheck(cfg, members)(context.Background(), sfid), "the client of the rollout flags is reused") | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.