forked from inventree/InvenTree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_api.py
More file actions
200 lines (160 loc) · 6.48 KB
/
test_api.py
File metadata and controls
200 lines (160 loc) · 6.48 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
"""Tests for general API tests for the plugin app."""
from django.urls import reverse
from rest_framework.exceptions import NotFound
from InvenTree.api_tester import InvenTreeAPITestCase, PluginMixin
from plugin.api import check_plugin
from plugin.models import PluginConfig
class PluginDetailAPITest(PluginMixin, InvenTreeAPITestCase):
"""Tests the plugin API endpoints."""
roles = [
'admin.add',
'admin.view',
'admin.change',
'admin.delete',
]
def setUp(self):
"""Setup for all tests."""
self.MSG_NO_PKG = 'Either packagename of URL must be provided'
self.PKG_NAME = 'minimal'
self.PKG_URL = 'git+https://github.com/geoffrey-a-reed/minimal'
super().setUp()
def test_plugin_install(self):
"""Test the plugin install command."""
url = reverse('api-plugin-install')
# valid - Pypi
data = self.post(
url,
{
'confirm': True,
'packagename': self.PKG_NAME
},
expected_code=201,
).data
self.assertEqual(data['success'], True)
# valid - github url
data = self.post(
url,
{
'confirm': True,
'url': self.PKG_URL
},
expected_code=201,
).data
self.assertEqual(data['success'], True)
# valid - github url and packagename
data = self.post(
url,
{
'confirm': True,
'url': self.PKG_URL,
'packagename': 'minimal',
},
expected_code=201,
).data
self.assertEqual(data['success'], True)
# invalid tries
# no input
self.post(url, {}, expected_code=400)
# no package info
data = self.post(url, {
'confirm': True,
}, expected_code=400).data
self.assertEqual(data['url'][0].title().upper(), self.MSG_NO_PKG.upper())
self.assertEqual(data['packagename'][0].title().upper(), self.MSG_NO_PKG.upper())
# not confirmed
self.post(url, {
'packagename': self.PKG_NAME
}, expected_code=400)
data = self.post(url, {
'packagename': self.PKG_NAME,
'confirm': False,
}, expected_code=400).data
self.assertEqual(data['confirm'][0].title().upper(), 'Installation not confirmed'.upper())
def test_plugin_activate(self):
"""Test the plugin activate."""
test_plg = self.plugin_confs.first()
def assert_plugin_active(self, active):
self.assertEqual(PluginConfig.objects.all().first().active, active)
# Should not work - not a superuser
response = self.client.post(reverse('api-plugin-activate'), {}, follow=True)
self.assertEqual(response.status_code, 403)
# Make user superuser
self.user.is_superuser = True
self.user.save()
# Deactivate plugin
test_plg.active = False
test_plg.save()
# Activate plugin with detail url
assert_plugin_active(self, False)
response = self.client.patch(reverse('api-plugin-detail-activate', kwargs={'pk': test_plg.id}), {}, follow=True)
self.assertEqual(response.status_code, 200)
assert_plugin_active(self, True)
# Deactivate plugin
test_plg.active = False
test_plg.save()
# Activate plugin
assert_plugin_active(self, False)
response = self.client.patch(reverse('api-plugin-activate'), {'pk': test_plg.pk}, follow=True)
self.assertEqual(response.status_code, 200)
assert_plugin_active(self, True)
def test_admin_action(self):
"""Test the PluginConfig action commands."""
url = reverse('admin:plugin_pluginconfig_changelist')
test_plg = self.plugin_confs.first()
# deactivate plugin
response = self.client.post(url, {
'action': 'plugin_deactivate',
'index': 0,
'_selected_action': [test_plg.pk],
}, follow=True)
self.assertEqual(response.status_code, 200)
# deactivate plugin - deactivate again -> nothing will hapen but the nothing 'changed' function is triggered
response = self.client.post(url, {
'action': 'plugin_deactivate',
'index': 0,
'_selected_action': [test_plg.pk],
}, follow=True)
self.assertEqual(response.status_code, 200)
# activate plugin
response = self.client.post(url, {
'action': 'plugin_activate',
'index': 0,
'_selected_action': [test_plg.pk],
}, follow=True)
self.assertEqual(response.status_code, 200)
# save to deactivate a plugin
response = self.client.post(reverse('admin:plugin_pluginconfig_change', args=(test_plg.pk, )), {
'_save': 'Save',
}, follow=True)
self.assertEqual(response.status_code, 200)
def test_model(self):
"""Test the PluginConfig model."""
# check mixin registry
plg = self.plugin_confs.first()
mixin_dict = plg.mixins()
self.assertIn('base', mixin_dict)
self.assertDictContainsSubset({'base': {'key': 'base', 'human_name': 'base'}}, mixin_dict)
# check reload on save
with self.assertWarns(Warning) as cm:
plg_inactive = self.plugin_confs.filter(active=False).first()
plg_inactive.active = True
plg_inactive.save()
self.assertEqual(cm.warning.args[0], 'A reload was triggered')
def test_check_plugin(self):
"""Test check_plugin function."""
# No argument
with self.assertRaises(NotFound) as exc:
check_plugin(plugin_slug=None, plugin_pk=None)
self.assertEqual(str(exc.exception.detail), 'Plugin not specified')
# Wrong with slug
with self.assertRaises(NotFound) as exc:
check_plugin(plugin_slug='123abc', plugin_pk=None)
self.assertEqual(str(exc.exception.detail), "Plugin '123abc' not installed")
# Wrong with pk
with self.assertRaises(NotFound) as exc:
check_plugin(plugin_slug=None, plugin_pk='123')
self.assertEqual(str(exc.exception.detail), "Plugin '123' not installed")
# Not active
with self.assertRaises(NotFound) as exc:
check_plugin(plugin_slug='inventreebarcode', plugin_pk=None)
self.assertEqual(str(exc.exception.detail), "Plugin 'inventreebarcode' is not active")