/
test_register.py
131 lines (111 loc) · 4.83 KB
/
test_register.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# -*- Mode:Python; indent-tabs-mode:nil; tab-width:4 -*-
#
# Copyright (C) 2016-2017 Canonical Ltd
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from simplejson.scanner import JSONDecodeError
from unittest import mock
from testtools.matchers import Contains, Equals, Not
from snapcraft import storeapi
from . import FakeStoreCommandsBaseTestCase
class RegisterTestCase(FakeStoreCommandsBaseTestCase):
def test_register_without_name_must_error(self):
result = self.run_command(["register"])
self.assertThat(result.exit_code, Equals(2))
self.assertThat(result.output, Contains("Usage:"))
def test_register_without_login_must_ask(self):
self.fake_store_register.mock.side_effect = [
storeapi.errors.InvalidCredentialsError("error"),
None,
]
result = self.run_command(
["register", "snap-name"], input="y\nuser@example.com\nsecret\n"
)
self.assertThat(
result.output, Contains("You are required to login before continuing.")
)
def test_register_name_successfully(self):
result = self.run_command(["register", "test-snap"], input="y\n")
self.assertThat(result.exit_code, Equals(0))
self.assertThat(result.output, Contains("Registering test-snap"))
self.assertThat(
result.output,
Contains("Congrats! You are now the publisher of 'test-snap'."),
)
self.assertThat(
result.output,
Not(Contains("Congratulations! You're now the publisher for 'test-snap'.")),
)
self.fake_store_register.mock.assert_called_once_with(
"test-snap", is_private=False, series="16", store_id=None
)
def test_register_name_to_specific_store_successfully(self):
result = self.run_command(
["register", "test-snap", "--store", "my-brand"], input="y\n"
)
self.assertThat(result.exit_code, Equals(0))
self.assertThat(result.output, Contains("Registering test-snap"))
self.assertThat(
result.output,
Contains("Congrats! You are now the publisher of 'test-snap'."),
)
self.assertThat(
result.output,
Not(Contains("Congratulations! You're now the publisher for 'test-snap'.")),
)
self.fake_store_register.mock.assert_called_once_with(
"test-snap", is_private=False, series="16", store_id="my-brand"
)
def test_register_private_name_successfully(self):
result = self.run_command(["register", "test-snap", "--private"], input="y\n")
self.assertThat(result.exit_code, Equals(0))
self.assertThat(
result.output,
Contains("Even though this is private snap, you should think carefully"),
)
self.assertThat(result.output, Contains("Registering test-snap"))
self.assertThat(
result.output,
Contains("Congrats! You are now the publisher of 'test-snap'."),
)
self.assertThat(
result.output,
Not(Contains("Congratulations! You're now the publisher for 'test-snap'.")),
)
self.fake_store_register.mock.assert_called_once_with(
"test-snap", is_private=True, series="16", store_id=None
)
def test_registration_failed(self):
response = mock.Mock()
response.json.side_effect = JSONDecodeError("mock-fail", "doc", 1)
self.fake_store_register.mock.side_effect = storeapi.errors.StoreRegistrationError(
"test-snap", response
)
raised = self.assertRaises(
storeapi.errors.StoreRegistrationError,
self.run_command,
["register", "test-snap"],
input="y\n",
)
self.assertThat(str(raised), Equals("Registration failed."))
def test_registration_cancelled(self):
response = mock.Mock()
response.json.side_effect = JSONDecodeError("mock-fail", "doc", 1)
self.fake_store_register.mock.side_effect = storeapi.errors.StoreRegistrationError(
"test-snap", response
)
result = self.run_command(["register", "test-snap"], input="n\n")
self.assertThat(result.exit_code, Equals(0))
self.assertThat(
result.output, Contains("Thank you! 'test-snap' will remain available")
)