|
| 1 | +/* |
| 2 | +Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | +contributor license agreements. See the NOTICE file distributed with |
| 4 | +this work for additional information regarding copyright ownership. |
| 5 | +The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | +(the "License"); you may not use this file except in compliance with |
| 7 | +the License. You may obtain a copy of the License at |
| 8 | +
|
| 9 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +
|
| 11 | +Unless required by applicable law or agreed to in writing, software |
| 12 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +See the License for the specific language governing permissions and |
| 15 | +limitations under the License. |
| 16 | +*/ |
| 17 | + |
| 18 | +package api |
| 19 | + |
| 20 | +import ( |
| 21 | + "testing" |
| 22 | + |
| 23 | + mockplugin "github.com/apache/incubator-devlake/mocks/core/plugin" |
| 24 | + |
| 25 | + "github.com/apache/incubator-devlake/core/models/common" |
| 26 | + "github.com/apache/incubator-devlake/core/plugin" |
| 27 | + "github.com/apache/incubator-devlake/helpers/pluginhelper/api" |
| 28 | + "github.com/apache/incubator-devlake/helpers/srvhelper" |
| 29 | + "github.com/apache/incubator-devlake/plugins/bitbucket/models" |
| 30 | + "github.com/stretchr/testify/assert" |
| 31 | +) |
| 32 | + |
| 33 | +func mockBitbucketPlugin(t *testing.T) { |
| 34 | + mockMeta := mockplugin.NewPluginMeta(t) |
| 35 | + mockMeta.On("RootPkgPath").Return("github.com/apache/incubator-devlake/plugins/bitbucket") |
| 36 | + mockMeta.On("Name").Return("dummy").Maybe() |
| 37 | + err := plugin.RegisterPlugin("bitbucket", mockMeta) |
| 38 | + assert.Equal(t, err, nil) |
| 39 | +} |
| 40 | + |
| 41 | +func TestMakeScopes(t *testing.T) { |
| 42 | + mockBitbucketPlugin(t) |
| 43 | + |
| 44 | + const connectionId uint64 = 1 |
| 45 | + const bitbucketId = "owner/repo" |
| 46 | + const expectDomainScopeId = "bitbucket:BitbucketRepo:1:owner/repo" |
| 47 | + |
| 48 | + actualScopes, err := makeScopesV200( |
| 49 | + []*srvhelper.ScopeDetail[models.BitbucketRepo, models.BitbucketScopeConfig]{ |
| 50 | + { |
| 51 | + Scope: models.BitbucketRepo{ |
| 52 | + Scope: common.Scope{ |
| 53 | + ConnectionId: connectionId, |
| 54 | + }, |
| 55 | + BitbucketId: bitbucketId, |
| 56 | + }, |
| 57 | + ScopeConfig: &models.BitbucketScopeConfig{ |
| 58 | + ScopeConfig: common.ScopeConfig{ |
| 59 | + Entities: []string{plugin.DOMAIN_TYPE_CODE, plugin.DOMAIN_TYPE_TICKET, plugin.DOMAIN_TYPE_CICD}, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + &models.BitbucketConnection{ |
| 65 | + BaseConnection: api.BaseConnection{ |
| 66 | + Model: common.Model{ |
| 67 | + ID: connectionId, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + ) |
| 72 | + assert.Nil(t, err) |
| 73 | + assert.Equal(t, 3, len(actualScopes)) |
| 74 | + assert.Equal(t, expectDomainScopeId, actualScopes[0].ScopeId()) |
| 75 | + assert.Equal(t, expectDomainScopeId, actualScopes[1].ScopeId()) |
| 76 | + assert.Equal(t, expectDomainScopeId, actualScopes[2].ScopeId()) |
| 77 | +} |
| 78 | + |
| 79 | +func TestMakeScopesWithEmptyEntities(t *testing.T) { |
| 80 | + mockBitbucketPlugin(t) |
| 81 | + |
| 82 | + const connectionId uint64 = 1 |
| 83 | + const bitbucketId = "owner/repo" |
| 84 | + const expectDomainScopeId = "bitbucket:BitbucketRepo:1:owner/repo" |
| 85 | + |
| 86 | + actualScopes, err := makeScopesV200( |
| 87 | + []*srvhelper.ScopeDetail[models.BitbucketRepo, models.BitbucketScopeConfig]{ |
| 88 | + { |
| 89 | + Scope: models.BitbucketRepo{ |
| 90 | + Scope: common.Scope{ |
| 91 | + ConnectionId: connectionId, |
| 92 | + }, |
| 93 | + BitbucketId: bitbucketId, |
| 94 | + }, |
| 95 | + ScopeConfig: &models.BitbucketScopeConfig{ |
| 96 | + ScopeConfig: common.ScopeConfig{ |
| 97 | + Entities: []string{}, |
| 98 | + }, |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + &models.BitbucketConnection{ |
| 103 | + BaseConnection: api.BaseConnection{ |
| 104 | + Model: common.Model{ |
| 105 | + ID: connectionId, |
| 106 | + }, |
| 107 | + }, |
| 108 | + }, |
| 109 | + ) |
| 110 | + assert.Nil(t, err) |
| 111 | + // empty entities should default to all domain types, producing repo + cicd + board scopes |
| 112 | + assert.Equal(t, 3, len(actualScopes)) |
| 113 | + assert.Equal(t, expectDomainScopeId, actualScopes[0].ScopeId()) |
| 114 | +} |
| 115 | + |
| 116 | +func TestMakeScopesWithCrossEntity(t *testing.T) { |
| 117 | + mockBitbucketPlugin(t) |
| 118 | + |
| 119 | + const connectionId uint64 = 1 |
| 120 | + const bitbucketId = "owner/repo" |
| 121 | + const expectDomainScopeId = "bitbucket:BitbucketRepo:1:owner/repo" |
| 122 | + |
| 123 | + actualScopes, err := makeScopesV200( |
| 124 | + []*srvhelper.ScopeDetail[models.BitbucketRepo, models.BitbucketScopeConfig]{ |
| 125 | + { |
| 126 | + Scope: models.BitbucketRepo{ |
| 127 | + Scope: common.Scope{ |
| 128 | + ConnectionId: connectionId, |
| 129 | + }, |
| 130 | + BitbucketId: bitbucketId, |
| 131 | + }, |
| 132 | + ScopeConfig: &models.BitbucketScopeConfig{ |
| 133 | + ScopeConfig: common.ScopeConfig{ |
| 134 | + Entities: []string{plugin.DOMAIN_TYPE_CROSS, plugin.DOMAIN_TYPE_TICKET}, |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + &models.BitbucketConnection{ |
| 140 | + BaseConnection: api.BaseConnection{ |
| 141 | + Model: common.Model{ |
| 142 | + ID: connectionId, |
| 143 | + }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + ) |
| 147 | + assert.Nil(t, err) |
| 148 | + // CROSS entity should trigger repo scope creation, plus ticket = board scope |
| 149 | + assert.Equal(t, 2, len(actualScopes)) |
| 150 | + assert.Equal(t, expectDomainScopeId, actualScopes[0].ScopeId()) |
| 151 | + assert.Equal(t, "repos", actualScopes[0].TableName()) |
| 152 | + assert.Equal(t, "boards", actualScopes[1].TableName()) |
| 153 | +} |
0 commit comments