3
3
from unittest import TestCase
4
4
from mock import patch , call , Mock
5
5
6
- from botocore .exceptions import ClientError
7
- from serverlessrepo .exceptions import ServerlessRepoError
6
+ from serverlessrepo .exceptions import ServerlessRepoError , InvalidS3UriError
8
7
from serverlessrepo .publish import CREATE_APPLICATION , UPDATE_APPLICATION
8
+ from serverlessrepo .parser import METADATA , SERVERLESS_REPO_APPLICATION
9
9
10
- from samcli .commands .publish .command import do_cli as publish_cli
10
+ from samcli .commands .publish .command import do_cli as publish_cli , SEMANTIC_VERSION
11
11
from samcli .commands .exceptions import UserException
12
12
13
13
@@ -26,7 +26,7 @@ def setUp(self):
26
26
def test_must_raise_if_value_error (self , click_mock , get_template_data_mock ):
27
27
get_template_data_mock .side_effect = ValueError ("Template not found" )
28
28
with self .assertRaises (UserException ) as context :
29
- publish_cli (self .ctx_mock , self .template )
29
+ publish_cli (self .ctx_mock , self .template , None )
30
30
31
31
message = str (context .exception )
32
32
self .assertEqual ("Template not found" , message )
@@ -38,42 +38,20 @@ def test_must_raise_if_value_error(self, click_mock, get_template_data_mock):
38
38
def test_must_raise_if_serverlessrepo_error (self , click_mock , publish_application_mock ):
39
39
publish_application_mock .side_effect = ServerlessRepoError ()
40
40
with self .assertRaises (UserException ):
41
- publish_cli (self .ctx_mock , self .template )
41
+ publish_cli (self .ctx_mock , self .template , None )
42
42
43
43
click_mock .secho .assert_called_with ("Publish Failed" , fg = "red" )
44
44
45
45
@patch ('samcli.commands.publish.command.get_template_data' , Mock (return_value = {}))
46
46
@patch ('samcli.commands.publish.command.publish_application' )
47
47
@patch ('samcli.commands.publish.command.click' )
48
- def test_must_raise_if_s3_uri_error (self , click_mock , publish_application_mock ):
49
- publish_application_mock .side_effect = ClientError (
50
- {
51
- 'Error' : {
52
- 'Code' : 'BadRequestException' ,
53
- 'Message' : 'Invalid S3 URI'
54
- }
55
- },
56
- 'create_application'
57
- )
48
+ def test_must_raise_if_invalid_S3_uri_error (self , click_mock , publish_application_mock ):
49
+ publish_application_mock .side_effect = InvalidS3UriError (message = "" )
58
50
with self .assertRaises (UserException ) as context :
59
- publish_cli (self .ctx_mock , self .template )
51
+ publish_cli (self .ctx_mock , self .template , None )
60
52
61
53
message = str (context .exception )
62
- self .assertIn ("Please make sure that you have uploaded application artifacts "
63
- "to S3 by packaging the template" , message )
64
- click_mock .secho .assert_called_with ("Publish Failed" , fg = "red" )
65
-
66
- @patch ('samcli.commands.publish.command.get_template_data' , Mock (return_value = {}))
67
- @patch ('samcli.commands.publish.command.publish_application' )
68
- @patch ('samcli.commands.publish.command.click' )
69
- def test_must_raise_if_not_s3_uri_error (self , click_mock , publish_application_mock ):
70
- publish_application_mock .side_effect = ClientError (
71
- {'Error' : {'Code' : 'OtherError' , 'Message' : 'OtherMessage' }},
72
- 'other_operation'
73
- )
74
- with self .assertRaises (ClientError ):
75
- publish_cli (self .ctx_mock , self .template )
76
-
54
+ self .assertTrue ("Your SAM template contains invalid S3 URIs" in message )
77
55
click_mock .secho .assert_called_with ("Publish Failed" , fg = "red" )
78
56
79
57
@patch ('samcli.commands.publish.command.get_template_data' , Mock (return_value = {}))
@@ -86,7 +64,7 @@ def test_must_succeed_to_create_application(self, click_mock, publish_applicatio
86
64
'actions' : [CREATE_APPLICATION ]
87
65
}
88
66
89
- publish_cli (self .ctx_mock , self .template )
67
+ publish_cli (self .ctx_mock , self .template , None )
90
68
details_str = json .dumps ({'attr1' : 'value1' }, indent = 2 )
91
69
expected_msg = "Created new application with the following metadata:\n {}"
92
70
expected_link = self .console_link .format (
@@ -95,7 +73,7 @@ def test_must_succeed_to_create_application(self, click_mock, publish_applicatio
95
73
)
96
74
click_mock .secho .assert_has_calls ([
97
75
call ("Publish Succeeded" , fg = "green" ),
98
- call (expected_msg .format (details_str ), fg = "yellow" ),
76
+ call (expected_msg .format (details_str )),
99
77
call (expected_link , fg = "yellow" )
100
78
])
101
79
@@ -109,7 +87,7 @@ def test_must_succeed_to_update_application(self, click_mock, publish_applicatio
109
87
'actions' : [UPDATE_APPLICATION ]
110
88
}
111
89
112
- publish_cli (self .ctx_mock , self .template )
90
+ publish_cli (self .ctx_mock , self .template , None )
113
91
details_str = json .dumps ({'attr1' : 'value1' }, indent = 2 )
114
92
expected_msg = 'The following metadata of application "{}" has been updated:\n {}'
115
93
expected_link = self .console_link .format (
@@ -118,7 +96,7 @@ def test_must_succeed_to_update_application(self, click_mock, publish_applicatio
118
96
)
119
97
click_mock .secho .assert_has_calls ([
120
98
call ("Publish Succeeded" , fg = "green" ),
121
- call (expected_msg .format (self .application_id , details_str ), fg = "yellow" ),
99
+ call (expected_msg .format (self .application_id , details_str )),
122
100
call (expected_link , fg = "yellow" )
123
101
])
124
102
@@ -139,9 +117,49 @@ def test_print_console_link_if_context_region_not_set(self, click_mock, boto3_mo
139
117
session_mock .region_name = "us-west-1"
140
118
boto3_mock .Session .return_value = session_mock
141
119
142
- publish_cli (self .ctx_mock , self .template )
120
+ publish_cli (self .ctx_mock , self .template , None )
143
121
expected_link = self .console_link .format (
144
122
session_mock .region_name ,
145
123
self .application_id .replace ('/' , '~' )
146
124
)
147
125
click_mock .secho .assert_called_with (expected_link , fg = "yellow" )
126
+
127
+ @patch ('samcli.commands.publish.command.get_template_data' )
128
+ @patch ('samcli.commands.publish.command.publish_application' )
129
+ def test_must_use_template_semantic_version (self , publish_application_mock ,
130
+ get_template_data_mock ):
131
+ template_data = {
132
+ METADATA : {
133
+ SERVERLESS_REPO_APPLICATION : {SEMANTIC_VERSION : '0.1' }
134
+ }
135
+ }
136
+ get_template_data_mock .return_value = template_data
137
+ publish_application_mock .return_value = {
138
+ 'application_id' : self .application_id ,
139
+ 'details' : {}, 'actions' : {}
140
+ }
141
+ publish_cli (self .ctx_mock , self .template , None )
142
+ publish_application_mock .assert_called_with (template_data )
143
+
144
+ @patch ('samcli.commands.publish.command.get_template_data' )
145
+ @patch ('samcli.commands.publish.command.publish_application' )
146
+ def test_must_override_template_semantic_version (self , publish_application_mock ,
147
+ get_template_data_mock ):
148
+ template_data = {
149
+ METADATA : {
150
+ SERVERLESS_REPO_APPLICATION : {SEMANTIC_VERSION : '0.1' }
151
+ }
152
+ }
153
+ get_template_data_mock .return_value = template_data
154
+ publish_application_mock .return_value = {
155
+ 'application_id' : self .application_id ,
156
+ 'details' : {}, 'actions' : {}
157
+ }
158
+
159
+ publish_cli (self .ctx_mock , self .template , '0.2' )
160
+ expected_template_data = {
161
+ METADATA : {
162
+ SERVERLESS_REPO_APPLICATION : {SEMANTIC_VERSION : '0.2' }
163
+ }
164
+ }
165
+ publish_application_mock .assert_called_with (expected_template_data )
0 commit comments