Skip to content

Commit

Permalink
Added unit tests to createAzApp function
Browse files Browse the repository at this point in the history
  • Loading branch information
Vidya2606 committed May 16, 2024
1 parent d0b16a1 commit 5d0a300
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 6 deletions.
1 change: 0 additions & 1 deletion pkg/providers/azure.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
114 changes: 109 additions & 5 deletions pkg/providers/azure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
}
1 change: 1 addition & 0 deletions pkg/providers/mock/az-client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 5d0a300

Please sign in to comment.