|
| 1 | +// Copyright (c) 2021 Terminus, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package endpoints |
| 16 | + |
| 17 | +import ( |
| 18 | + "reflect" |
| 19 | + "testing" |
| 20 | + |
| 21 | + "bou.ke/monkey" |
| 22 | + |
| 23 | + "github.com/erda-project/erda/apistructs" |
| 24 | + "github.com/erda-project/erda/modules/core-services/model" |
| 25 | + "github.com/erda-project/erda/modules/core-services/services/application" |
| 26 | + "github.com/erda-project/erda/modules/core-services/services/project" |
| 27 | +) |
| 28 | + |
| 29 | +func TestEndpoints_buildScopeInfo(t *testing.T) { |
| 30 | + type args struct { |
| 31 | + accessReq apistructs.ScopeRoleAccessRequest |
| 32 | + permission apistructs.PermissionList |
| 33 | + } |
| 34 | + tests := []struct { |
| 35 | + name string |
| 36 | + args args |
| 37 | + want apistructs.PermissionList |
| 38 | + wantErr bool |
| 39 | + }{ |
| 40 | + { |
| 41 | + name: "test_app_name", |
| 42 | + args: args{ |
| 43 | + accessReq: apistructs.ScopeRoleAccessRequest{ |
| 44 | + Scope: apistructs.Scope{ |
| 45 | + Type: "app", |
| 46 | + ID: "1", |
| 47 | + }, |
| 48 | + }, |
| 49 | + permission: apistructs.PermissionList{}, |
| 50 | + }, |
| 51 | + want: apistructs.PermissionList{ |
| 52 | + ScopeInfo: &apistructs.ScopeInfo{ |
| 53 | + ProjectName: "test", |
| 54 | + AppName: "test", |
| 55 | + }, |
| 56 | + }, |
| 57 | + wantErr: false, |
| 58 | + }, |
| 59 | + } |
| 60 | + for _, tt := range tests { |
| 61 | + t.Run(tt.name, func(t *testing.T) { |
| 62 | + e := &Endpoints{} |
| 63 | + |
| 64 | + var app = &application.Application{} |
| 65 | + |
| 66 | + patch1 := monkey.PatchInstanceMethod(reflect.TypeOf(app), "Get", func(app *application.Application, applicationID int64) (*model.Application, error) { |
| 67 | + return &model.Application{ |
| 68 | + Name: "test", |
| 69 | + ProjectID: 1, |
| 70 | + }, nil |
| 71 | + }) |
| 72 | + defer patch1.Unpatch() |
| 73 | + |
| 74 | + var pj = &project.Project{} |
| 75 | + patch2 := monkey.PatchInstanceMethod(reflect.TypeOf(pj), "GetModelProject", func(project *project.Project, projectID int64) (*model.Project, error) { |
| 76 | + return &model.Project{ |
| 77 | + DisplayName: "test", |
| 78 | + }, nil |
| 79 | + }) |
| 80 | + defer patch2.Unpatch() |
| 81 | + |
| 82 | + e.app = app |
| 83 | + e.project = pj |
| 84 | + |
| 85 | + got, err := e.buildScopeInfo(tt.args.accessReq, tt.args.permission) |
| 86 | + if (err != nil) != tt.wantErr { |
| 87 | + t.Errorf("buildScopeInfo() error = %v, wantErr %v", err, tt.wantErr) |
| 88 | + return |
| 89 | + } |
| 90 | + if !reflect.DeepEqual(got, tt.want) { |
| 91 | + t.Errorf("buildScopeInfo() got = %v, want %v", got, tt.want) |
| 92 | + } |
| 93 | + }) |
| 94 | + } |
| 95 | +} |
0 commit comments