github.com/SAP/jenkins-library@v1.362.0/cmd/ascAppUpload_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package cmd
     5  
     6  import (
     7  	"fmt"
     8  	"github.com/SAP/jenkins-library/pkg/asc"
     9  	"github.com/SAP/jenkins-library/pkg/mock"
    10  	"github.com/pkg/errors"
    11  	"github.com/stretchr/testify/assert"
    12  	"testing"
    13  	"time"
    14  )
    15  
    16  type ascAppUploadMockUtils struct {
    17  	*mock.ExecMockRunner
    18  	*mock.FilesMock
    19  }
    20  
    21  func newAscAppUploadTestsUtils() ascAppUploadMockUtils {
    22  	utils := ascAppUploadMockUtils{
    23  		ExecMockRunner: &mock.ExecMockRunner{},
    24  		FilesMock:      &mock.FilesMock{},
    25  	}
    26  	return utils
    27  }
    28  
    29  type ascSystemMock struct {
    30  	app                        asc.App
    31  	appError                   error
    32  	createReleaseResponse      asc.CreateReleaseResponse
    33  	createReleaseResponseError error
    34  	jamfAppInfo                asc.JamfAppInformationResponse
    35  	jamfAppInfoError           error
    36  	uploadIpaError             error
    37  }
    38  
    39  func (sys *ascSystemMock) GetAppById(appId string) (asc.App, error) {
    40  	return sys.app, sys.appError
    41  }
    42  
    43  func (sys *ascSystemMock) CreateRelease(ascAppId int, version string, description string, releaseDate string, visible bool) (asc.CreateReleaseResponse, error) {
    44  	return sys.createReleaseResponse, sys.createReleaseResponseError
    45  }
    46  
    47  func (sys *ascSystemMock) GetJamfAppInfo(bundleId string, jamfTargetSystem string) (asc.JamfAppInformationResponse, error) {
    48  	return sys.jamfAppInfo, sys.jamfAppInfoError
    49  }
    50  
    51  func (sys *ascSystemMock) UploadIpa(path string, jamfAppId int, jamfTargetSystem string, bundleId string, ascRelease asc.Release) error {
    52  	return sys.uploadIpaError
    53  }
    54  
    55  func TestRunAscAppUpload(t *testing.T) {
    56  	t.Parallel()
    57  
    58  	t.Run("succesfull upload", func(t *testing.T) {
    59  		t.Parallel()
    60  		// init
    61  		config := ascAppUploadOptions{
    62  			FilePath:         "./sample-app.ipa",
    63  			JamfTargetSystem: "test",
    64  			AppID:            "1",
    65  		}
    66  
    67  		utils := newAscAppUploadTestsUtils()
    68  		utils.AddFile("sample-app.ipa", []byte("dummy content"))
    69  
    70  		ascClient := &ascSystemMock{
    71  			app: asc.App{
    72  				AppId:    1,
    73  				AppName:  "Sample App",
    74  				BundleId: "sample.bundle.id",
    75  				JamfId:   "1",
    76  			},
    77  			createReleaseResponse: asc.CreateReleaseResponse{
    78  				Status: "success",
    79  				Data:   asc.Release{ReleaseID: 1, AppID: 1, Version: "version", Description: "description", ReleaseDate: time.Now(), Visible: true},
    80  			},
    81  			jamfAppInfo: asc.JamfAppInformationResponse{
    82  				MobileDeviceApplication: asc.JamfMobileDeviceApplication{
    83  					General: asc.JamfMobileDeviceApplicationGeneral{
    84  						Id: 1,
    85  					},
    86  				},
    87  			},
    88  		}
    89  		// test
    90  		err := runAscAppUpload(&config, nil, utils, ascClient)
    91  
    92  		// assert
    93  		assert.NoError(t, err)
    94  	})
    95  
    96  	t.Run("error during release creation", func(t *testing.T) {
    97  		t.Parallel()
    98  		// init
    99  		config := ascAppUploadOptions{
   100  			FilePath:         "./sample-app.ipa",
   101  			JamfTargetSystem: "test",
   102  			AppID:            "1",
   103  		}
   104  
   105  		utils := newAscAppUploadTestsUtils()
   106  
   107  		errorMessage := "Error while creating release"
   108  
   109  		ascClient := &ascSystemMock{
   110  			app: asc.App{
   111  				AppId:    1,
   112  				AppName:  "Sample App",
   113  				BundleId: "sample.bundle.id",
   114  				JamfId:   "1",
   115  			},
   116  			createReleaseResponse: asc.CreateReleaseResponse{Status: "failure", Message: errorMessage},
   117  		}
   118  		// test
   119  		err := runAscAppUpload(&config, nil, utils, ascClient)
   120  
   121  		// assert
   122  		assert.EqualError(t, err, errorMessage)
   123  	})
   124  
   125  	t.Run("error while fetching jamf app info", func(t *testing.T) {
   126  		t.Parallel()
   127  		// init
   128  		config := ascAppUploadOptions{
   129  			FilePath:         "./sample-app.ipa",
   130  			JamfTargetSystem: "test",
   131  			AppID:            "1",
   132  		}
   133  
   134  		utils := newAscAppUploadTestsUtils()
   135  
   136  		errorMessage := "Error while fetching jamf app info"
   137  
   138  		ascClient := &ascSystemMock{
   139  			app: asc.App{
   140  				AppId:    1,
   141  				AppName:  "Sample App",
   142  				BundleId: "sample.bundle.id",
   143  				JamfId:   "1",
   144  			},
   145  			createReleaseResponse: asc.CreateReleaseResponse{Status: "success", Data: asc.Release{ReleaseID: 1}},
   146  			jamfAppInfoError:      errors.New(errorMessage),
   147  		}
   148  		// test
   149  		err := runAscAppUpload(&config, nil, utils, ascClient)
   150  
   151  		// assert
   152  		assert.EqualError(t, err, fmt.Sprintf("failed to get jamf app info: %s", errorMessage))
   153  	})
   154  
   155  	t.Run("error if jamf app id is 0", func(t *testing.T) {
   156  		t.Parallel()
   157  		// init
   158  		config := ascAppUploadOptions{
   159  			FilePath:         "./sample-app.ipa",
   160  			JamfTargetSystem: "test",
   161  			AppID:            "1",
   162  		}
   163  
   164  		utils := newAscAppUploadTestsUtils()
   165  
   166  		ascClient := &ascSystemMock{
   167  			app: asc.App{
   168  				AppId:    1,
   169  				AppName:  "Sample App",
   170  				BundleId: "sample.bundle.id",
   171  				JamfId:   "1",
   172  			},
   173  			createReleaseResponse: asc.CreateReleaseResponse{Status: "success", Data: asc.Release{ReleaseID: 1}},
   174  			jamfAppInfo: asc.JamfAppInformationResponse{
   175  				MobileDeviceApplication: asc.JamfMobileDeviceApplication{
   176  					General: asc.JamfMobileDeviceApplicationGeneral{
   177  						Id: 0,
   178  					},
   179  				},
   180  			},
   181  		}
   182  		// test
   183  		err := runAscAppUpload(&config, nil, utils, ascClient)
   184  
   185  		// assert
   186  		assert.EqualError(t, err, fmt.Sprintf("failed to get jamf app id"))
   187  	})
   188  }