From efcd5787f47656f7dff512b884f0fd6dcb5fbae0 Mon Sep 17 00:00:00 2001 From: Vidya Reddy <59590642+Vidya2606@users.noreply.github.com> Date: Thu, 2 May 2024 10:43:51 -0700 Subject: [PATCH 1/8] Added SDK calls for createAzApp --- pkg/providers/azure.go | 44 ++++++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 21 deletions(-) diff --git a/pkg/providers/azure.go b/pkg/providers/azure.go index d1f3affd..36f5907a 100644 --- a/pkg/providers/azure.go +++ b/pkg/providers/azure.go @@ -5,7 +5,11 @@ import ( "encoding/json" "errors" "fmt" + "github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud" "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription" + "github.com/Azure/draft/pkg/cred" + msgraph "github.com/microsoftgraph/msgraph-sdk-go" + graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models" "os/exec" "time" @@ -45,7 +49,7 @@ func InitiateAzureOIDCFlow(ctx context.Context, sc *SetUpCmd, s spinner.Spinner) if AzAppExists(sc.AppName) { return errors.New("app already exists") - } else if err := sc.createAzApp(); err != nil { + } else if err := sc.createAzApp(ctx); err != nil { return err } @@ -85,38 +89,36 @@ func InitiateAzureOIDCFlow(ctx context.Context, sc *SetUpCmd, s spinner.Spinner) return nil } -func (sc *SetUpCmd) createAzApp() error { +func (sc *SetUpCmd) createAzApp(ctx context.Context) error { log.Debug("Commencing Azure app creation...") start := time.Now() log.Debug(start) createApp := func() error { - createAppCmd := exec.Command("az", "ad", "app", "create", "--only-show-errors", "--display-name", sc.AppName) - - out, err := createAppCmd.CombinedOutput() + azCred, err := cred.GetCred() if err != nil { - log.Printf("%s\n", out) - return err + return fmt.Errorf("getting credentials: %w", err) } + graphClient, _ := msgraph.NewGraphServiceClientWithCredentials(azCred, []string{cloud.AzurePublic.Services[cloud.ResourceManager].Endpoint + "/.default"}) - if AzAppExists(sc.AppName) { - var azApp map[string]interface{} - if err := json.Unmarshal(out, &azApp); err != nil { - return err - } - appId := fmt.Sprint(azApp["appId"]) - - sc.appId = appId + requestBody := graphmodels.NewApplication() + displayName := sc.AppName + requestBody.SetDisplayName(&displayName) - end := time.Since(start) - log.Debug("App created successfully!") - log.Debug(end) - return nil + application, err := graphClient.Applications().Post(ctx, requestBody, nil) + if err != nil { + return fmt.Errorf("creating Azure app: %v", err) } - return errors.New("app creation time has exceeded max elapsed time for exponential backoff") - } + appId := *application.GetId() + + sc.appId = appId + end := time.Since(start) + log.Debug("App created successfully!") + log.Debug(end) + return nil + } backoff := bo.NewExponentialBackOff() backoff.MaxElapsedTime = 5 * time.Second From 844f807ea341ba896ef67b0d8075e495ea8b97f2 Mon Sep 17 00:00:00 2001 From: Vidya Reddy <59590642+Vidya2606@users.noreply.github.com> Date: Tue, 14 May 2024 10:15:52 -0700 Subject: [PATCH 2/8] changes to the graphClient interface and getAppObjectId func --- cmd/setup-gh.go | 2 +- pkg/providers/az-client.go | 14 ++--- pkg/providers/azure.go | 17 ++---- pkg/providers/azure_test.go | 95 ++++++++++++++++++--------------- pkg/providers/mock/az-client.go | 19 ++++--- 5 files changed, 71 insertions(+), 76 deletions(-) diff --git a/cmd/setup-gh.go b/cmd/setup-gh.go index e75ad9f5..cb8a0fd3 100644 --- a/cmd/setup-gh.go +++ b/cmd/setup-gh.go @@ -81,7 +81,7 @@ func createGraphClient(azCred *azidentity.DefaultAzureCredential) (providers.Gra if err != nil { return nil, fmt.Errorf("creating graph service client: %w", err) } - return &providers.GraphServiceClient{Client: client}, nil + return client, nil } func fillSetUpConfig(sc *providers.SetUpCmd) { diff --git a/pkg/providers/az-client.go b/pkg/providers/az-client.go index 0c483435..08ac8420 100644 --- a/pkg/providers/az-client.go +++ b/pkg/providers/az-client.go @@ -7,6 +7,7 @@ import ( "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription" msgraph "github.com/microsoftgraph/msgraph-sdk-go" + graphapp "github.com/microsoftgraph/msgraph-sdk-go/applications" ) type AzClient struct { @@ -19,19 +20,14 @@ type azTenantClient interface { NewListPager(options *armsubscription.TenantsClientListOptions) *runtime.Pager[armsubscription.TenantsClientListResponse] } -// GraphServiceClient implements the GraphClient interface. -type GraphServiceClient struct { - Client *msgraph.GraphServiceClient -} - type GraphClient interface { - GetApplicationObjectId(ctx context.Context, appId string) (string, error) + Applications() *graphapp.ApplicationsRequestBuilder } -var _ GraphClient = &GraphServiceClient{} +var _ GraphClient = &msgraph.GraphServiceClient{} -func (g *GraphServiceClient) GetApplicationObjectId(ctx context.Context, appId string) (string, error) { - req := g.Client.Applications().ByApplicationId(appId) +func GetApplicationObjectId(ctx context.Context, appId string, graphClient GraphClient) (string, error) { + req := graphClient.Applications().ByApplicationId(appId) app, err := req.Get(ctx, nil) if err != nil { diff --git a/pkg/providers/azure.go b/pkg/providers/azure.go index 36f5907a..570c60fd 100644 --- a/pkg/providers/azure.go +++ b/pkg/providers/azure.go @@ -5,10 +5,7 @@ import ( "encoding/json" "errors" "fmt" - "github.com/Azure/azure-sdk-for-go/sdk/azcore/cloud" "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription" - "github.com/Azure/draft/pkg/cred" - msgraph "github.com/microsoftgraph/msgraph-sdk-go" graphmodels "github.com/microsoftgraph/msgraph-sdk-go/models" "os/exec" "time" @@ -95,24 +92,16 @@ func (sc *SetUpCmd) createAzApp(ctx context.Context) error { log.Debug(start) createApp := func() error { - azCred, err := cred.GetCred() - if err != nil { - return fmt.Errorf("getting credentials: %w", err) - } - graphClient, _ := msgraph.NewGraphServiceClientWithCredentials(azCred, []string{cloud.AzurePublic.Services[cloud.ResourceManager].Endpoint + "/.default"}) - requestBody := graphmodels.NewApplication() displayName := sc.AppName requestBody.SetDisplayName(&displayName) - application, err := graphClient.Applications().Post(ctx, requestBody, nil) + application, err := sc.AzClient.GraphClient.Applications().Post(ctx, requestBody, nil) if err != nil { return fmt.Errorf("creating Azure app: %v", err) } - appId := *application.GetId() - - sc.appId = appId + sc.appId = *application.GetId() end := time.Since(start) log.Debug("App created successfully!") @@ -316,7 +305,7 @@ func (sc *SetUpCmd) createFederatedCredentials() error { func (sc *SetUpCmd) getAppObjectId(ctx context.Context) error { log.Debug("Fetching Azure application object ID") - appID, err := sc.AzClient.GraphClient.GetApplicationObjectId(ctx, sc.appId) + appID, err := GetApplicationObjectId(ctx, sc.appId, sc.AzClient.GraphClient) if err != nil { return fmt.Errorf("getting application object Id: %w", err) } diff --git a/pkg/providers/azure_test.go b/pkg/providers/azure_test.go index 6cc3c284..a76d3639 100644 --- a/pkg/providers/azure_test.go +++ b/pkg/providers/azure_test.go @@ -7,6 +7,10 @@ import ( "github.com/Azure/azure-sdk-for-go/sdk/azcore/tracing" "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription" mock_providers "github.com/Azure/draft/pkg/providers/mock" + abstractions "github.com/microsoft/kiota-abstractions-go" + "github.com/microsoft/kiota-abstractions-go/serialization" + graphapp "github.com/microsoftgraph/msgraph-sdk-go/applications" + "github.com/microsoftgraph/msgraph-sdk-go/models" "go.uber.org/mock/gomock" "strings" "testing" @@ -138,18 +142,44 @@ func TestGetTenantId_NilTenantInList(t *testing.T) { } } +var testAppID = "mockAppID" +var errToSend error = nil + +type mockActionable struct { + models.Applicationable +} + +func (m *mockActionable) GetAppId() *string { + return &testAppID +} + +type mockRequestAdapter struct { + abstractions.RequestAdapter +} + +func (m *mockRequestAdapter) Send(context.Context, *abstractions.RequestInformation, serialization.ParsableFactory, abstractions.ErrorMappings) (serialization.Parsable, error) { + return &mockActionable{}, errToSend +} + func TestGetAppObjectId(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() mockGraphClient := mock_providers.NewMockGraphClient(ctrl) - appID := "testAppID" - expectedAppID := "mockAppID" - mockGraphClient.EXPECT().GetApplicationObjectId(gomock.Any(), appID).Return(expectedAppID, nil) + mockAppRequestBuilder := &graphapp.ApplicationsRequestBuilder{ + BaseRequestBuilder: abstractions.BaseRequestBuilder{ + PathParameters: map[string]string{"key": "value"}, + RequestAdapter: &mockRequestAdapter{}, + UrlTemplate: "dummyUrlTemplate", + }, + } + testAppID = "mockAppID" + errToSend = nil + mockGraphClient.EXPECT().Applications().Return(mockAppRequestBuilder).AnyTimes() sc := &SetUpCmd{ - appId: appID, + appId: testAppID, AzClient: AzClient{ GraphClient: mockGraphClient, }, @@ -160,23 +190,27 @@ func TestGetAppObjectId(t *testing.T) { if err != nil { t.Errorf("Expected no error, got: %v", err) } - if sc.appObjectId != expectedAppID { - t.Errorf("Expected application ID %s, got: %s", expectedAppID, sc.appObjectId) - } } +// Test case - when the GraphClient returns an empty application ID: func TestGetAppObjectId_Error(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() mockGraphClient := mock_providers.NewMockGraphClient(ctrl) - appID := "testAppID" - expectedError := errors.New("mock error") - mockGraphClient.EXPECT().GetApplicationObjectId(gomock.Any(), appID).Return("", expectedError) + mockAppRequestBuilder := &graphapp.ApplicationsRequestBuilder{ + BaseRequestBuilder: abstractions.BaseRequestBuilder{ + PathParameters: map[string]string{"key": "value"}, + RequestAdapter: &mockRequestAdapter{}, + UrlTemplate: "dummyUrlTemplate", + }, + } + testAppID = "" + mockGraphClient.EXPECT().Applications().Return(mockAppRequestBuilder).AnyTimes() sc := &SetUpCmd{ - appId: appID, + appId: testAppID, AzClient: AzClient{ GraphClient: mockGraphClient, }, @@ -196,50 +230,27 @@ func TestGetAppObjectId_ErrorFromGraphClient(t *testing.T) { mockGraphClient := mock_providers.NewMockGraphClient(ctrl) - appID := "testAppID" - expectedError := errors.New("mock error") - mockGraphClient.EXPECT().GetApplicationObjectId(gomock.Any(), appID).Return("", expectedError) - - sc := &SetUpCmd{ - appId: appID, - AzClient: AzClient{ - GraphClient: mockGraphClient, + mockAppRequestBuilder := &graphapp.ApplicationsRequestBuilder{ + BaseRequestBuilder: abstractions.BaseRequestBuilder{ + PathParameters: map[string]string{"key": "value"}, + RequestAdapter: &mockRequestAdapter{}, + UrlTemplate: "dummyUrlTemplate", }, } - err := sc.getAppObjectId(context.Background()) - if err == nil { - t.Error("Expected an error, got nil") - } expectedErrorMsg := "getting application object Id: mock error" - if err.Error() != expectedErrorMsg { - t.Errorf("Expected error message '%s', got '%s'", expectedErrorMsg, err.Error()) - } -} - -// Test case - when the GraphClient returns an empty application ID: -func TestGetAppObjectId_EmptyAppIdFromGraphClient(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - mockGraphClient := mock_providers.NewMockGraphClient(ctrl) - - appID := "testAppID" - expectedError := errors.New("application object ID is empty") - mockGraphClient.EXPECT().GetApplicationObjectId(gomock.Any(), appID).Return("", expectedError) + errToSend = errors.New(expectedErrorMsg) + mockGraphClient.EXPECT().Applications().Return(mockAppRequestBuilder).AnyTimes() sc := &SetUpCmd{ - appId: appID, + appId: "testAppID", AzClient: AzClient{ GraphClient: mockGraphClient, }, } err := sc.getAppObjectId(context.Background()) - if err == nil { t.Error("Expected an error, got nil") - } else if !strings.Contains(err.Error(), expectedError.Error()) { - t.Errorf("Expected error '%v', got '%v'", expectedError, err) } } diff --git a/pkg/providers/mock/az-client.go b/pkg/providers/mock/az-client.go index bcf69751..0f395ebe 100644 --- a/pkg/providers/mock/az-client.go +++ b/pkg/providers/mock/az-client.go @@ -10,11 +10,11 @@ package mock_providers import ( - context "context" reflect "reflect" runtime "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" armsubscription "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription" + applications "github.com/microsoftgraph/msgraph-sdk-go/applications" gomock "go.uber.org/mock/gomock" ) @@ -78,17 +78,16 @@ func (m *MockGraphClient) EXPECT() *MockGraphClientMockRecorder { return m.recorder } -// GetApplicationObjectId mocks base method. -func (m *MockGraphClient) GetApplicationObjectId(ctx context.Context, appId string) (string, error) { +// Applications mocks base method. +func (m *MockGraphClient) Applications() *applications.ApplicationsRequestBuilder { m.ctrl.T.Helper() - ret := m.ctrl.Call(m, "GetApplicationObjectId", ctx, appId) - ret0, _ := ret[0].(string) - ret1, _ := ret[1].(error) - return ret0, ret1 + ret := m.ctrl.Call(m, "Applications") + ret0, _ := ret[0].(*applications.ApplicationsRequestBuilder) + return ret0 } -// GetApplicationObjectId indicates an expected call of GetApplicationObjectId. -func (mr *MockGraphClientMockRecorder) GetApplicationObjectId(ctx, appId any) *gomock.Call { +// Applications indicates an expected call of Applications. +func (mr *MockGraphClientMockRecorder) Applications() *gomock.Call { mr.mock.ctrl.T.Helper() - return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetApplicationObjectId", reflect.TypeOf((*MockGraphClient)(nil).GetApplicationObjectId), ctx, appId) + return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Applications", reflect.TypeOf((*MockGraphClient)(nil).Applications)) } From dfdbeed39775dca2f926166b517af2e6e01d7b31 Mon Sep 17 00:00:00 2001 From: Vidya Reddy <59590642+Vidya2606@users.noreply.github.com> Date: Tue, 14 May 2024 15:04:32 -0700 Subject: [PATCH 3/8] updated unit tests for getAppObjectId function using the table driven tests --- pkg/providers/azure_test.go | 130 ++++++++++++++---------------------- 1 file changed, 50 insertions(+), 80 deletions(-) diff --git a/pkg/providers/azure_test.go b/pkg/providers/azure_test.go index a76d3639..7582ac3f 100644 --- a/pkg/providers/azure_test.go +++ b/pkg/providers/azure_test.go @@ -162,95 +162,65 @@ func (m *mockRequestAdapter) Send(context.Context, *abstractions.RequestInformat } func TestGetAppObjectId(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - mockGraphClient := mock_providers.NewMockGraphClient(ctrl) - - mockAppRequestBuilder := &graphapp.ApplicationsRequestBuilder{ - BaseRequestBuilder: abstractions.BaseRequestBuilder{ - PathParameters: map[string]string{"key": "value"}, - RequestAdapter: &mockRequestAdapter{}, - UrlTemplate: "dummyUrlTemplate", - }, - } - testAppID = "mockAppID" - errToSend = nil - mockGraphClient.EXPECT().Applications().Return(mockAppRequestBuilder).AnyTimes() - - sc := &SetUpCmd{ - appId: testAppID, - AzClient: AzClient{ - GraphClient: mockGraphClient, + tests := []struct { + name string + testAppID string + errToSend error + expectErr bool + }{ + { + name: "Success", + testAppID: "mockAppID", + errToSend: nil, + expectErr: false, }, - } - - err := sc.getAppObjectId(context.Background()) - - if err != nil { - t.Errorf("Expected no error, got: %v", err) - } -} - -// Test case - when the GraphClient returns an empty application ID: -func TestGetAppObjectId_Error(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - mockGraphClient := mock_providers.NewMockGraphClient(ctrl) - - mockAppRequestBuilder := &graphapp.ApplicationsRequestBuilder{ - BaseRequestBuilder: abstractions.BaseRequestBuilder{ - PathParameters: map[string]string{"key": "value"}, - RequestAdapter: &mockRequestAdapter{}, - UrlTemplate: "dummyUrlTemplate", + { + name: "EmptyAppID", + testAppID: "", + errToSend: nil, + expectErr: true, }, - } - testAppID = "" - mockGraphClient.EXPECT().Applications().Return(mockAppRequestBuilder).AnyTimes() - - sc := &SetUpCmd{ - appId: testAppID, - AzClient: AzClient{ - GraphClient: mockGraphClient, + { + name: "ErrorFromGraphClient", + testAppID: "testAppID", + errToSend: errors.New("getting application object Id: mock error"), + expectErr: true, }, } - err := sc.getAppObjectId(context.Background()) - - if err == nil { - t.Error("Expected an error, got nil") - } -} - -// Test case - when the GraphClient returns an error -func TestGetAppObjectId_ErrorFromGraphClient(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() - mockGraphClient := mock_providers.NewMockGraphClient(ctrl) + mockGraphClient := mock_providers.NewMockGraphClient(ctrl) - mockAppRequestBuilder := &graphapp.ApplicationsRequestBuilder{ - BaseRequestBuilder: abstractions.BaseRequestBuilder{ - PathParameters: map[string]string{"key": "value"}, - RequestAdapter: &mockRequestAdapter{}, - UrlTemplate: "dummyUrlTemplate", - }, - } + mockAppRequestBuilder := &graphapp.ApplicationsRequestBuilder{ + BaseRequestBuilder: abstractions.BaseRequestBuilder{ + PathParameters: map[string]string{"key": "value"}, + RequestAdapter: &mockRequestAdapter{}, + UrlTemplate: "dummyUrlTemplate", + }, + } - expectedErrorMsg := "getting application object Id: mock error" - errToSend = errors.New(expectedErrorMsg) - mockGraphClient.EXPECT().Applications().Return(mockAppRequestBuilder).AnyTimes() + testAppID = tt.testAppID + errToSend = tt.errToSend + mockGraphClient.EXPECT().Applications().Return(mockAppRequestBuilder).AnyTimes() - sc := &SetUpCmd{ - appId: "testAppID", - AzClient: AzClient{ - GraphClient: mockGraphClient, - }, - } + sc := &SetUpCmd{ + appId: tt.testAppID, + AzClient: AzClient{ + GraphClient: mockGraphClient, + }, + } - err := sc.getAppObjectId(context.Background()) - if err == nil { - t.Error("Expected an error, got nil") + err := sc.getAppObjectId(context.Background()) + if tt.expectErr && err == nil { + t.Error("Expected an error, got nil") + } + if !tt.expectErr && err != nil { + t.Errorf("Expected no error, got: %v", err) + } + }) } } From d0b16a1d27cd48e52216ec4f7d5a61e627345ed7 Mon Sep 17 00:00:00 2001 From: Brandon Foley Date: Thu, 16 May 2024 11:28:03 -0400 Subject: [PATCH 4/8] Release v34 (#280) --- CHANGELOG.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 42f0d997..800509d2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,20 @@ # Change Log +## [0.0.34] - 2024-05-16 + +### Added + +- [#277](https://github.com/Azure/draft/pull/277) Updates/Fixes for draft. Adds generator label to manifests +- [#275](https://github.com/Azure/draft/pull/275) Add NOTICE file +- [#274](https://github.com/Azure/draft/pull/274) gomodule multistage build +- [#273](https://github.com/Azure/draft/pull/273) finishing safeguard additions +- [#272](https://github.com/Azure/draft/pull/272) sdk calls for assignsprole +- [#271](https://github.com/Azure/draft/pull/271) update draft to go 1.22 +- [#269](https://github.com/Azure/draft/pull/269) yaml file extension validation +- [#262](https://github.com/Azure/draft/pull/262) sdk calls for getTenantID +- [#242](https://github.com/Azure/draft/pull/242) changes in correlation with new GH action permission changes + + ## [0.0.33] - 2023-08-07 ### Added From 5d0a3005c2a43c82be916f8bb4e049e3cb6815d0 Mon Sep 17 00:00:00 2001 From: Vidya Reddy <59590642+Vidya2606@users.noreply.github.com> Date: Thu, 16 May 2024 16:16:59 -0700 Subject: [PATCH 5/8] Added unit tests to createAzApp function --- pkg/providers/azure.go | 1 - pkg/providers/azure_test.go | 114 ++++++++++++++++++++++++++++++-- pkg/providers/mock/az-client.go | 1 + 3 files changed, 110 insertions(+), 6 deletions(-) diff --git a/pkg/providers/azure.go b/pkg/providers/azure.go index a7f7d20c..eba40085 100644 --- a/pkg/providers/azure.go +++ b/pkg/providers/azure.go @@ -101,7 +101,6 @@ func (sc *SetUpCmd) createAzApp(ctx context.Context) error { if err != nil { return fmt.Errorf("creating Azure app: %v", err) } - sc.appId = *application.GetId() end := time.Since(start) diff --git a/pkg/providers/azure_test.go b/pkg/providers/azure_test.go index 73c0a317..c147196f 100644 --- a/pkg/providers/azure_test.go +++ b/pkg/providers/azure_test.go @@ -7,11 +7,13 @@ import ( "github.com/Azure/azure-sdk-for-go/sdk/azcore/tracing" "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization" "github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/subscription/armsubscription" + graphapp "github.com/microsoftgraph/msgraph-sdk-go/applications" + mock_providers "github.com/Azure/draft/pkg/providers/mock" abstractions "github.com/microsoft/kiota-abstractions-go" "github.com/microsoft/kiota-abstractions-go/serialization" - graphapp "github.com/microsoftgraph/msgraph-sdk-go/applications" "github.com/microsoftgraph/msgraph-sdk-go/models" + "go.uber.org/mock/gomock" "strings" "testing" @@ -144,22 +146,60 @@ func TestGetTenantId_NilTenantInList(t *testing.T) { } var testAppID = "mockAppID" +var testID = "mockID" var errToSend error = nil -type mockActionable struct { +type mockApplicationable struct { models.Applicationable } -func (m *mockActionable) GetAppId() *string { +func (m *mockApplicationable) GetAppId() *string { return &testAppID } +func (m *mockApplicationable) GetId() *string { + return &testID +} + +type mockSerialWriter struct { + serialization.SerializationWriter +} + +func (m *mockSerialWriter) GetSerializedContent() ([]byte, error) { + content := []byte("a few bytes") + return content, nil +} + +func (m *mockSerialWriter) Close() error { + return nil +} + +func (m *mockSerialWriter) WriteObjectValue(string, serialization.Parsable, ...serialization.Parsable) error { + return nil +} + +type mockSerialWriterFactory struct { + serialization.SerializationWriterFactory +} + +func (m *mockSerialWriterFactory) GetSerializationWriter(string) (serialization.SerializationWriter, error) { + return &mockSerialWriter{}, nil +} + type mockRequestAdapter struct { abstractions.RequestAdapter } -func (m *mockRequestAdapter) Send(context.Context, *abstractions.RequestInformation, serialization.ParsableFactory, abstractions.ErrorMappings) (serialization.Parsable, error) { - return &mockActionable{}, errToSend +func (m *mockRequestAdapter) Send( + context.Context, + *abstractions.RequestInformation, + serialization.ParsableFactory, + abstractions.ErrorMappings) (serialization.Parsable, error) { + return &mockApplicationable{}, errToSend +} + +func (m *mockRequestAdapter) GetSerializationWriterFactory() serialization.SerializationWriterFactory { + return &mockSerialWriterFactory{} } func TestGetAppObjectId(t *testing.T) { @@ -288,3 +328,67 @@ func TestAssignSpRole(t *testing.T) { }) } } + +func TestCreateAzApp(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + mockGraphClient := mock_providers.NewMockGraphClient(ctrl) + + mockAppRequestBuilder := &graphapp.ApplicationsRequestBuilder{ + BaseRequestBuilder: abstractions.BaseRequestBuilder{ + PathParameters: map[string]string{"key": "value"}, + RequestAdapter: &mockRequestAdapter{}, + UrlTemplate: "dummyUrlTemplate", + }, + } + + mockGraphClient.EXPECT().Applications().Return(mockAppRequestBuilder).AnyTimes() + + sc := &SetUpCmd{ + AzClient: AzClient{ + GraphClient: mockGraphClient, + }, + AppName: "AppName", + } + + err := sc.createAzApp(context.Background()) + + if err != nil { + t.Errorf("Expected no error, got: %v", err) + } +} + +func TestCreateAzApp_ErrorCreatingApp(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() + + mockGraphClient := mock_providers.NewMockGraphClient(ctrl) + + errToSend = errors.New("getting application object Id: mock error") + + mockAppRequestBuilder := &graphapp.ApplicationsRequestBuilder{ + BaseRequestBuilder: abstractions.BaseRequestBuilder{ + PathParameters: map[string]string{"key": "value"}, + RequestAdapter: &mockRequestAdapter{}, + UrlTemplate: "dummyUrlTemplate", + }, + } + + expectedErr := errors.New("creating Azure app: getting application object Id: mock error") + + mockGraphClient.EXPECT().Applications().Return(mockAppRequestBuilder).AnyTimes() + + sc := &SetUpCmd{ + AzClient: AzClient{ + GraphClient: mockGraphClient, + }, + AppName: "", + } + + err := sc.createAzApp(context.Background()) + + if err == nil || err.Error() != expectedErr.Error() { + t.Errorf("Expected error %v, got: %v", expectedErr, err) + } +} diff --git a/pkg/providers/mock/az-client.go b/pkg/providers/mock/az-client.go index 08212141..a5172191 100644 --- a/pkg/providers/mock/az-client.go +++ b/pkg/providers/mock/az-client.go @@ -10,6 +10,7 @@ package mock_providers import ( + context "context" reflect "reflect" runtime "github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime" From c161d1440e73627ed88cd88e634f8a8eb3c0a91b Mon Sep 17 00:00:00 2001 From: Vidya Reddy <59590642+Vidya2606@users.noreply.github.com> Date: Fri, 17 May 2024 14:14:41 -0700 Subject: [PATCH 6/8] Updated the unit test --- pkg/providers/azure_test.go | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/pkg/providers/azure_test.go b/pkg/providers/azure_test.go index c147196f..32114f11 100644 --- a/pkg/providers/azure_test.go +++ b/pkg/providers/azure_test.go @@ -359,14 +359,13 @@ func TestCreateAzApp(t *testing.T) { } } +// Empty App name func TestCreateAzApp_ErrorCreatingApp(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() mockGraphClient := mock_providers.NewMockGraphClient(ctrl) - errToSend = errors.New("getting application object Id: mock error") - mockAppRequestBuilder := &graphapp.ApplicationsRequestBuilder{ BaseRequestBuilder: abstractions.BaseRequestBuilder{ PathParameters: map[string]string{"key": "value"}, @@ -375,8 +374,7 @@ func TestCreateAzApp_ErrorCreatingApp(t *testing.T) { }, } - expectedErr := errors.New("creating Azure app: getting application object Id: mock error") - + errToSend = errors.New("creating Azure app: mock error") mockGraphClient.EXPECT().Applications().Return(mockAppRequestBuilder).AnyTimes() sc := &SetUpCmd{ @@ -387,8 +385,7 @@ func TestCreateAzApp_ErrorCreatingApp(t *testing.T) { } err := sc.createAzApp(context.Background()) - - if err == nil || err.Error() != expectedErr.Error() { - t.Errorf("Expected error %v, got: %v", expectedErr, err) + if err == nil { + t.Errorf("Expected error 'creating Azure app: mock error', got nil") } } From 9023a769c616c32bf369b6e6228620649f504b15 Mon Sep 17 00:00:00 2001 From: Vidya Reddy <59590642+Vidya2606@users.noreply.github.com> Date: Mon, 20 May 2024 12:02:10 -0700 Subject: [PATCH 7/8] Updating unit tests --- pkg/providers/azure_test.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/providers/azure_test.go b/pkg/providers/azure_test.go index 32114f11..f731bba7 100644 --- a/pkg/providers/azure_test.go +++ b/pkg/providers/azure_test.go @@ -333,6 +333,11 @@ func TestCreateAzApp(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() + // Test setup + errToSend = nil // no error expected + testAppID = "mockAppID" + testID = "mockID" + mockGraphClient := mock_providers.NewMockGraphClient(ctrl) mockAppRequestBuilder := &graphapp.ApplicationsRequestBuilder{ @@ -364,6 +369,11 @@ func TestCreateAzApp_ErrorCreatingApp(t *testing.T) { ctrl := gomock.NewController(t) defer ctrl.Finish() + // Test setup + errToSend = errors.New("creating Azure app: mock error") + testAppID = "mockAppID" + testID = "mockID" + mockGraphClient := mock_providers.NewMockGraphClient(ctrl) mockAppRequestBuilder := &graphapp.ApplicationsRequestBuilder{ @@ -374,7 +384,6 @@ func TestCreateAzApp_ErrorCreatingApp(t *testing.T) { }, } - errToSend = errors.New("creating Azure app: mock error") mockGraphClient.EXPECT().Applications().Return(mockAppRequestBuilder).AnyTimes() sc := &SetUpCmd{ From 32c80bfa94cb3e1d54ed1c6c8268f0e651632e24 Mon Sep 17 00:00:00 2001 From: Vidya Reddy <59590642+Vidya2606@users.noreply.github.com> Date: Wed, 22 May 2024 11:05:35 -0700 Subject: [PATCH 8/8] updated unit tests --- pkg/providers/azure_test.go | 101 ++++++++++++++++-------------------- 1 file changed, 44 insertions(+), 57 deletions(-) diff --git a/pkg/providers/azure_test.go b/pkg/providers/azure_test.go index f731bba7..890873d3 100644 --- a/pkg/providers/azure_test.go +++ b/pkg/providers/azure_test.go @@ -330,71 +330,58 @@ func TestAssignSpRole(t *testing.T) { } func TestCreateAzApp(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - // Test setup - errToSend = nil // no error expected - testAppID = "mockAppID" - testID = "mockID" - - mockGraphClient := mock_providers.NewMockGraphClient(ctrl) - - mockAppRequestBuilder := &graphapp.ApplicationsRequestBuilder{ - BaseRequestBuilder: abstractions.BaseRequestBuilder{ - PathParameters: map[string]string{"key": "value"}, - RequestAdapter: &mockRequestAdapter{}, - UrlTemplate: "dummyUrlTemplate", + tests := []struct { + name string + appName string + errToSend error + expectErr bool + }{ + { + name: "Success", + appName: "AppName", + errToSend: nil, + expectErr: false, }, - } - - mockGraphClient.EXPECT().Applications().Return(mockAppRequestBuilder).AnyTimes() - - sc := &SetUpCmd{ - AzClient: AzClient{ - GraphClient: mockGraphClient, + { + name: "ErrorCreatingApp", + appName: "", + errToSend: errors.New("creating Azure app: mock error"), + expectErr: true, }, - AppName: "AppName", } - err := sc.createAzApp(context.Background()) - - if err != nil { - t.Errorf("Expected no error, got: %v", err) - } -} - -// Empty App name -func TestCreateAzApp_ErrorCreatingApp(t *testing.T) { - ctrl := gomock.NewController(t) - defer ctrl.Finish() - - // Test setup - errToSend = errors.New("creating Azure app: mock error") - testAppID = "mockAppID" - testID = "mockID" + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctrl := gomock.NewController(t) + defer ctrl.Finish() - mockGraphClient := mock_providers.NewMockGraphClient(ctrl) + errToSend = tt.errToSend + mockGraphClient := mock_providers.NewMockGraphClient(ctrl) - mockAppRequestBuilder := &graphapp.ApplicationsRequestBuilder{ - BaseRequestBuilder: abstractions.BaseRequestBuilder{ - PathParameters: map[string]string{"key": "value"}, - RequestAdapter: &mockRequestAdapter{}, - UrlTemplate: "dummyUrlTemplate", - }, - } + mockAppRequestBuilder := &graphapp.ApplicationsRequestBuilder{ + BaseRequestBuilder: abstractions.BaseRequestBuilder{ + PathParameters: map[string]string{"key": "value"}, + RequestAdapter: &mockRequestAdapter{}, + UrlTemplate: "dummyUrlTemplate", + }, + } - mockGraphClient.EXPECT().Applications().Return(mockAppRequestBuilder).AnyTimes() + mockGraphClient.EXPECT().Applications().Return(mockAppRequestBuilder).AnyTimes() - sc := &SetUpCmd{ - AzClient: AzClient{ - GraphClient: mockGraphClient, - }, - AppName: "", - } + sc := &SetUpCmd{ + AzClient: AzClient{ + GraphClient: mockGraphClient, + }, + AppName: tt.appName, + } - err := sc.createAzApp(context.Background()) - if err == nil { - t.Errorf("Expected error 'creating Azure app: mock error', got nil") + err := sc.createAzApp(context.Background()) + if tt.expectErr && err == nil { + t.Errorf("Expected error, got nil") + } + if !tt.expectErr && err != nil { + t.Errorf("Expected no error, got: %v", err) + } + }) } }