github.com/rainforestapp/rainforest-cli@v2.12.0+incompatible/mobile_upload_test.go (about)

     1  package main
     2  
     3  import (
     4  	"strings"
     5  	"testing"
     6  
     7  	"github.com/rainforestapp/rainforest-cli/rainforest"
     8  	"github.com/urfave/cli"
     9  )
    10  
    11  const (
    12  	testMobileAppPath = "test/testing.zip"
    13  )
    14  
    15  type fakeMobileUploadAPI struct {
    16  	getPresignedPOST      func(fileExt string, siteID int, environmentID int, appSlot int) (*rainforest.RFPresignedPostData, error)
    17  	uploadToS3            func(postData *rainforest.RFPresignedPostData, filePath string) error
    18  	setSiteEnvironmentURL func(siteID int, environmentID int, appSlot int, newURL string) error
    19  }
    20  
    21  func (f fakeMobileUploadAPI) GetPresignedPOST(fileExt string, siteID int, environmentID int, appSlot int) (*rainforest.RFPresignedPostData, error) {
    22  	if f.getPresignedPOST != nil {
    23  		return f.getPresignedPOST(fileExt, siteID, environmentID, appSlot)
    24  	}
    25  	return nil, nil
    26  }
    27  
    28  func (f fakeMobileUploadAPI) UploadToS3(postData *rainforest.RFPresignedPostData, filePath string) error {
    29  	if f.uploadToS3 != nil {
    30  		return f.uploadToS3(postData, filePath)
    31  	}
    32  	return nil
    33  }
    34  
    35  func (f fakeMobileUploadAPI) UpdateURL(siteID int, environmentID int, appSlot int, newURL string) error {
    36  	if f.setSiteEnvironmentURL != nil {
    37  		return f.setSiteEnvironmentURL(siteID, environmentID, appSlot, newURL)
    38  	}
    39  	return nil
    40  }
    41  
    42  func TestUploadMobileApp(t *testing.T) {
    43  	siteID := 123
    44  	environmentID := 456
    45  	appSlot := 2
    46  
    47  	callCount := make(map[string]int)
    48  	f := fakeMobileUploadAPI{
    49  		getPresignedPOST: func(fileExt string, siteID int, environmentID int, appSlot int) (*rainforest.RFPresignedPostData, error) {
    50  			callCount["getPresignedPOST"] = callCount["getPresignedPOST"] + 1
    51  			return &rainforest.RFPresignedPostData{}, nil
    52  		},
    53  		uploadToS3: func(postData *rainforest.RFPresignedPostData, filePath string) error {
    54  			callCount["uploadToS3"] = callCount["uploadToS3"] + 1
    55  			return nil
    56  		},
    57  		setSiteEnvironmentURL: func(siteID int, environmentID int, appSlot int, newURL string) error {
    58  			callCount["setSiteEnvironmentURL"] = callCount["setSiteEnvironmentURL"] + 1
    59  			return nil
    60  		},
    61  	}
    62  
    63  	err := uploadMobileApp(f, testMobileAppPath, siteID, environmentID, appSlot)
    64  	if err != nil {
    65  		t.Errorf("Unexpected error: %v", err.Error())
    66  	}
    67  	if expected := 1; callCount["uploadToS3"] != expected {
    68  		t.Errorf("api.uploadToS3 called invalid number of times: %v, expected %v", callCount["uploadToS3"], expected)
    69  	}
    70  	if expected := 1; callCount["getPresignedPOST"] != expected {
    71  		t.Errorf("api.getPresignedPOST called invalid number of times: %v, expected %v", callCount["getPresignedPOST"], expected)
    72  	}
    73  	if expected := 1; callCount["setSiteEnvironmentURL"] != expected {
    74  		t.Errorf("api.setSiteEnvironmentURL called invalid number of times: %v, expected %v", callCount["setSiteEnvironmentURL"], expected)
    75  	}
    76  }
    77  func TestMobileAppUpload(t *testing.T) {
    78  	siteID := "123"
    79  	environmentID := "456"
    80  	appSlot := "2"
    81  
    82  	callCount := make(map[string]int)
    83  	f := fakeMobileUploadAPI{
    84  		getPresignedPOST: func(fileExt string, siteID int, environmentID int, appSlot int) (*rainforest.RFPresignedPostData, error) {
    85  			callCount["getPresignedPOST"] = callCount["getPresignedPOST"] + 1
    86  			return &rainforest.RFPresignedPostData{}, nil
    87  		},
    88  		uploadToS3: func(postData *rainforest.RFPresignedPostData, filePath string) error {
    89  			callCount["uploadToS3"] = callCount["uploadToS3"] + 1
    90  			return nil
    91  		},
    92  		setSiteEnvironmentURL: func(siteID int, environmentID int, appSlot int, newURL string) error {
    93  			callCount["setSiteEnvironmentURL"] = callCount["setSiteEnvironmentURL"] + 1
    94  			return nil
    95  		},
    96  	}
    97  	fakeContext := newFakeContext(map[string]interface{}{
    98  		"site-id":        siteID,
    99  		"environment-id": environmentID,
   100  		"app-slot":       appSlot,
   101  	}, cli.Args{testMobileAppPath})
   102  
   103  	err := mobileAppUpload(fakeContext, f)
   104  	if err != nil {
   105  		t.Errorf("Unexpected error: %v", err.Error())
   106  	}
   107  	if expected := 1; callCount["uploadToS3"] != expected {
   108  		t.Errorf("api.uploadToS3 called invalid number of times: %v, expected %v", callCount["uploadToS3"], expected)
   109  	}
   110  	if expected := 1; callCount["getPresignedPOST"] != expected {
   111  		t.Errorf("api.getPresignedPOST called invalid number of times: %v, expected %v", callCount["getPresignedPOST"], expected)
   112  	}
   113  	if expected := 1; callCount["setSiteEnvironmentURL"] != expected {
   114  		t.Errorf("api.setSiteEnvironmentURL called invalid number of times: %v, expected %v", callCount["setSiteEnvironmentURL"], expected)
   115  	}
   116  
   117  	// Bad extension
   118  	fakeContext = newFakeContext(map[string]interface{}{
   119  		"site-id":        siteID,
   120  		"environment-id": environmentID,
   121  	}, cli.Args{"./file.exe"})
   122  	err = mobileAppUpload(fakeContext, f)
   123  	if _, ok := err.(*cli.ExitError); !ok &&
   124  		!strings.Contains(err.Error(), "Invalid file extension") {
   125  		t.Errorf("Not erroring on invalid extension.")
   126  	}
   127  
   128  	// missing envorionment-id flag
   129  	fakeContext = newFakeContext(map[string]interface{}{
   130  		"site-id": siteID,
   131  	}, cli.Args{"./file.zip"})
   132  	err = mobileAppUpload(fakeContext, f)
   133  	if _, ok := err.(*cli.ExitError); !ok &&
   134  		!strings.Contains(err.Error(), "environment-id flag required") {
   135  		t.Errorf("Not erroring on missing environment-id.")
   136  	}
   137  
   138  	// non-int envorionment-id flag
   139  	fakeContext = newFakeContext(map[string]interface{}{
   140  		"site-id":         siteID,
   141  		"envorionment-id": "test",
   142  	}, cli.Args{"./file.zip"})
   143  	err = mobileAppUpload(fakeContext, f)
   144  	if _, ok := err.(*cli.ExitError); !ok &&
   145  		!strings.Contains(err.Error(), "environment-id must be an integer") {
   146  		t.Errorf("envorionment-id must be an integer.")
   147  	}
   148  
   149  	// missing site-id flag
   150  	fakeContext = newFakeContext(map[string]interface{}{
   151  		"environment-id": environmentID,
   152  	}, cli.Args{"./file.zip"})
   153  	err = mobileAppUpload(fakeContext, f)
   154  	if _, ok := err.(*cli.ExitError); !ok &&
   155  		!strings.Contains(err.Error(), "site-id flag required") {
   156  		t.Errorf("Not erroring on missing site-id.")
   157  	}
   158  
   159  	// non-int site-id flag
   160  	fakeContext = newFakeContext(map[string]interface{}{
   161  		"site-id":        "test",
   162  		"environment-id": environmentID,
   163  	}, cli.Args{"./file.zip"})
   164  	err = mobileAppUpload(fakeContext, f)
   165  	if _, ok := err.(*cli.ExitError); !ok &&
   166  		!strings.Contains(err.Error(), "site-id must be an integer") {
   167  		t.Errorf("Not erroring on missing site-id.")
   168  	}
   169  
   170  	// app-slot flag is set to a non-int
   171  	fakeContext = newFakeContext(map[string]interface{}{
   172  		"site-id":        siteID,
   173  		"environment-id": environmentID,
   174  		"app-slot":       "test",
   175  	}, cli.Args{"./file.zip"})
   176  	err = mobileAppUpload(fakeContext, f)
   177  	if _, ok := err.(*cli.ExitError); !ok &&
   178  		!strings.Contains(err.Error(), "app-slot must be an integer (1 to 100)") {
   179  		t.Errorf("Not erroring on invalid app-slot.")
   180  	}
   181  
   182  	// app-slot flag is set to a non 1-100 int
   183  	fakeContext = newFakeContext(map[string]interface{}{
   184  		"site-id":        siteID,
   185  		"environment-id": environmentID,
   186  		"app-slot":       101,
   187  	}, cli.Args{"./file.zip"})
   188  	err = mobileAppUpload(fakeContext, f)
   189  	if _, ok := err.(*cli.ExitError); !ok &&
   190  		!strings.Contains(err.Error(), "app-slot must be an integer (1 to 100)") {
   191  		t.Errorf("Not erroring on invalid app-slot.")
   192  	}
   193  
   194  	// missing filepath args
   195  	fakeContext = newFakeContext(map[string]interface{}{
   196  		"site-id":        siteID,
   197  		"environment-id": environmentID,
   198  	}, cli.Args{""})
   199  	err = mobileAppUpload(fakeContext, f)
   200  	if _, ok := err.(*cli.ExitError); !ok &&
   201  		!strings.Contains(err.Error(), "Mobile app filepath not specified") {
   202  		t.Errorf("Should have errored on missing filepath")
   203  	}
   204  
   205  	// bad filepath
   206  	fakeContext = newFakeContext(map[string]interface{}{
   207  		"site-id":        siteID,
   208  		"environment-id": environmentID,
   209  	}, cli.Args{"./bad_file_name.zip"})
   210  	err = mobileAppUpload(fakeContext, f)
   211  	if _, ok := err.(*cli.ExitError); !ok &&
   212  		!strings.Contains(err.Error(), "no such file or directory") {
   213  		t.Errorf("Should have errored on missing filepath")
   214  	}
   215  }