/
mock_azure_client.go
108 lines (87 loc) · 3.66 KB
/
mock_azure_client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package azuretestsupport
import (
"reflect"
"github.com/hexa-org/policy-mapper/api/policyprovider"
"github.com/hexa-org/policy-mapper/models/rar/testsupport/policytestsupport"
"github.com/hexa-org/policy-mapper/providers/azure/azad"
"github.com/hexa-org/policy-mapper/providers/azure/azuretestsupport/armtestsupport"
"github.com/stretchr/testify/mock"
)
type MockAzureClient struct {
mock.Mock
}
func (m *MockAzureClient) GetAzureApplications(key []byte) ([]azad.AzureWebApp, error) {
returnArgs := m.Called(key)
return returnArgs.Get(0).([]azad.AzureWebApp), returnArgs.Error(1)
}
func (m *MockAzureClient) GetWebApplications(key []byte) ([]policyprovider.ApplicationInfo, error) {
returnArgs := m.Called(key)
return returnArgs.Get(0).([]policyprovider.ApplicationInfo), returnArgs.Error(1)
}
func (m *MockAzureClient) GetServicePrincipals(key []byte, appId string) (azad.AzureServicePrincipals, error) {
returnArgs := m.Called(key, appId)
return returnArgs.Get(0).(azad.AzureServicePrincipals), returnArgs.Error(1)
}
func (m *MockAzureClient) GetUserInfoFromPrincipalId(key []byte, principalId string) (azad.AzureUser, error) {
returnArgs := m.Called(key, principalId)
return returnArgs.Get(0).(azad.AzureUser), returnArgs.Error(1)
}
func (m *MockAzureClient) GetPrincipalIdFromEmail(key []byte, email string) (string, error) {
returnArgs := m.Called(key, email)
return returnArgs.String(0), returnArgs.Error(1)
}
func (m *MockAzureClient) GetAppRoleAssignedTo(key []byte, servicePrincipalId string) (azad.AzureAppRoleAssignments, error) {
returnArgs := m.Called(key, servicePrincipalId)
return returnArgs.Get(0).(azad.AzureAppRoleAssignments), returnArgs.Error(1)
}
func (m *MockAzureClient) SetAppRoleAssignedTo(key []byte, servicePrincipalId string, assignments []azad.AzureAppRoleAssignment) error {
returnArgs := m.Called(key, servicePrincipalId, assignments)
return returnArgs.Error(0)
}
func NewMockAzureClient() *MockAzureClient {
return &MockAzureClient{}
}
func (m *MockAzureClient) ExpectGetAzureApplications() {
expApps := []azad.AzureWebApp{
{
ID: AzureAppId,
AppID: ServicePrincipalId,
Name: AzureAppName,
IdentifierUris: []string{armtestsupport.ApimServiceGatewayUrl},
},
}
m.On("GetAzureApplications", AzureKeyBytes()).Return(expApps, nil)
}
func (m *MockAzureClient) ExpectGetServicePrincipals() {
m.On("GetServicePrincipals", AzureKeyBytes(), AzureAppId).
Return(AzureServicePrincipals(), nil)
}
func (m *MockAzureClient) ExpectAppRoleAssignedTo(assignments []azad.AzureAppRoleAssignment) {
m.On("GetAppRoleAssignedTo", AzureKeyBytes(), ServicePrincipalId).
Return(MakeAssignments(assignments), nil)
}
func (m *MockAzureClient) ExpectGetUserInfoFromPrincipalId(principalIds ...string) {
for _, pId := range principalIds {
m.On("GetUserInfoFromPrincipalId", AzureKeyBytes(), pId).
Return(azad.AzureUser{
PrincipalId: pId,
Email: policytestsupport.MakeEmail(pId),
}, nil)
}
}
func (m *MockAzureClient) ExpectGetPrincipalIdFromEmail(email, principalId string) {
m.On("GetPrincipalIdFromEmail", AzureKeyBytes(), email).
Return(principalId, nil)
}
func (m *MockAzureClient) ExpectSetAppRoleAssignedTo(requestedAssignments []azad.AzureAppRoleAssignment) {
theFunc := mock.MatchedBy(func(actAssignments []azad.AzureAppRoleAssignment) bool {
if len(actAssignments) != len(requestedAssignments) {
return false
}
expSorted := SortAssignments(AssignmentsWithoutId(requestedAssignments))
actSorted := SortAssignments(AssignmentsWithoutId(actAssignments))
return reflect.DeepEqual(expSorted, actSorted)
})
m.On("SetAppRoleAssignedTo", AzureKeyBytes(), ServicePrincipalId, theFunc).
Return(nil)
}