FrontendApiTestCase.testDeleteNode()   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 8
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nop 1
1
# -*- coding: utf-8 -*-
2
3
import json
4
5
from django.contrib.auth.models import User
6
7
from ore.models import Notification
8
from .common import fixt_simple, OreLiveServerTestCase
9
10
11
class FrontendApiTestCase(OreLiveServerTestCase):
12
13
    """
14
        Tests for the Frontend API called from JavaScript.
15
    """
16
    fixtures = fixt_simple['files']
17
18
    baseUrl = '/api/front'
19
20
    def setUp(self):
21
        self.setUpLogin()
22
23
    # TODO: Test that session authentication is checked in the API implementation
24
    # TODO: Test that the user can only access his graphs, and not the ones of
25
    # other users
26
27
    def _testValidGraphJson(self, content):
28
        for key in ['id', 'seed', 'name', 'type',
29
                    'readOnly', 'nodes', 'edges', 'nodeGroups']:
30
            self.assertIn(key, content)
31
32
    def testGetGraph(self):
33
        for id, kind in fixt_simple['graphs'].iteritems():
34
            url = self.baseUrl + '/graphs/%u' % fixt_simple['pkFaultTree']
35
            response = self.ajaxGet(url)
36
            self.assertEqual(response.status_code, 200)
37
            content = json.loads(response.content)
38
            self._testValidGraphJson(content)
39
        response = self.ajaxGet(self.baseUrl + '/graphs/9999')
40
        self.assertEqual(response.status_code, 404)
41
42
    def testGraphDownload(self):
43
        for id, kind in fixt_simple['graphs'].iteritems():
44
            for format, test_str in [
45
                    ('graphml', '<graphml'), ('json', '{'), ('tex', '\\begin')]:
46
                url = self.baseUrl + \
47
                    '/graphs/%u?format=%s' % (
48
                        fixt_simple['pkFaultTree'],
49
                        format)
50
                response = self.ajaxGet(url)
51
                self.assertEqual(response.status_code, 200)
52
                self.assertIn(test_str, response.content)
53
54
    def testGetGraphs(self):
55
        url = self.baseUrl + '/graphs/'
56
        response = self.ajaxGet(url)
57
        self.assertEqual(response.status_code, 200)
58
        content = json.loads(response.content)
59
        assert ('graphs' in content)
60
61
    def testGraphFiltering(self):
62
        url = self.baseUrl + '/graphs/?kind=faulttree'
63
        response = self.ajaxGet(url)
64
        self.assertEqual(response.status_code, 200)
65
        content = json.loads(response.content)
66
        assert ('graphs' in content)
67
68
    def testCreateNode(self):
69
        initial_properties = {"name": "foo"}
70
        newnode = json.dumps({'y': 3,
71
                              'x': 7,
72
                              'kind': 'basicEvent',
73
                              'client_id': 888,
74
                              'properties': initial_properties})
75
76
        response = self.ajaxPost(self.baseUrl + '/graphs/%u/nodes/' % fixt_simple['pkFaultTree'],
77
                                 newnode,
78
                                 'application/json')
79
        self.assertEqual(response.status_code, 201)
80
#        newid = int(response['Location'].split('/')[-1])
81
#        newnode = Node.objects.get(client_id=newid, deleted=False)
82
#        self.assertItemsEqual(initial_properties, newnode.get_properties())
83
84
    def testCreateNodeGroup(self):
85
        nodes = [
86
            fixt_simple['clientIdAndGate'],
87
            fixt_simple['clientIdBasicEvent']]
88
        initial_properties = {"name": "foo"}
89
        newgroup = json.dumps(
90
            {'client_id': 999, 'nodeIds': nodes, "properties": initial_properties})
91
        response = self.ajaxPost(
92
            self.baseUrl + '/graphs/%u/nodegroups/' % fixt_simple['pkDFD'],
93
            newgroup,
94
            'application/json')
95
        self.assertEqual(response.status_code, 201)
96
        # print response['Location']
97
        # newid = int(response['Location'].split('/')[-1])
98
        # TODO: Doesn't work due to non-saving of LiveServerTestCase
99
        #       newgroup = NodeGroup.objects.get(client_id=newid, deleted=False)
100
        #       saved_nodes=newgroup.nodes.all()
101
        #       self.assertItemsEqual(nodes, saved_nodes)
102
103
        # Get complete graph and see if the node group is registered correctly
104
        url = self.baseUrl + '/graphs/%u' % fixt_simple['pkDFD']
105
        response = self.ajaxGet(url)
106
        self.assertEqual(response.status_code, 200)
107
        content = json.loads(response.content)
108
        for group in content['nodeGroups']:
109
            self.assertEqual(group['id'], 999)
110
            self.assertItemsEqual(group['nodeIds'], nodes)
111
            self.assertItemsEqual(group['properties'], initial_properties)
112
113
    def testDeleteNode(self):
114
        response = self.ajaxDelete(
115
            self.baseUrl +
116
            '/graphs/%u/nodes/%u' % (
117
                fixt_simple['pkFaultTree'],
118
                fixt_simple['clientIdBasicEvent'])
119
        )
120
        self.assertEqual(response.status_code, 204)
121
122
    def testDeleteNodeGroup(self):
123
        # TODO: Fixture should have a node group, instead of creating it here
124
        nodes = [
125
            fixt_simple['clientIdAndGate'],
126
            fixt_simple['clientIdBasicEvent']]
127
        newgroup = json.dumps({'client_id': 999, 'nodeIds': nodes})
128
        response = self.ajaxPost(self.baseUrl + '/graphs/%u/nodegroups/' % fixt_simple['pkDFD'],
129
                                 newgroup,
130
                                 'application/json')
131
        self.assertEqual(response.status_code, 201)
132
        newgroup = response['Location']
133
        # Try delete
134
        response = self.ajaxDelete(newgroup)
135
        self.assertEqual(response.status_code, 204)
136
137
    def testRelocateNode(self):
138
        newpos = json.dumps({'properties': {"y": 3, "x": 7}})
139
        response = self.ajaxPatch(
140
            self.baseUrl +
141
            '/graphs/%u/nodes/%u' % (
142
                fixt_simple['pkFaultTree'],
143
                fixt_simple['clientIdBasicEvent']),
144
            newpos,
145
            "application/json")
146
        self.assertEqual(response.status_code, 202)
147
148
    def testNodePropertyChange(self):
149
        newprop = json.dumps({"properties": {"name": "bar_öäü_1234"}})
150
        response = self.ajaxPatch(self.baseUrl + '/graphs/%u/nodes/%u' % (fixt_simple['pkFaultTree'], fixt_simple['clientIdBasicEvent']),
151
                                  newprop,
152
                                  "application/json")
153
        self.assertEqual(response.status_code, 202)
154
        # TODO: Fetch graph and check that the property is really stored
155
156
    def testNodeGroupPropertyChange(self):
157
        # TODO: Fixture should have a node group, instead of creating it here
158
        nodes = [
159
            fixt_simple['clientIdAndGate'],
160
            fixt_simple['clientIdBasicEvent']]
161
        newgroup = json.dumps({'client_id': 999, 'nodeIds': nodes})
162
        response = self.ajaxPost(self.baseUrl + '/graphs/%u/nodegroups/' % fixt_simple['pkDFD'],
163
                                 newgroup,
164
                                 'application/json')
165
        self.assertEqual(response.status_code, 201)
166
        newgroup = response['Location']
167
        # Try changing
168
        newprop = json.dumps({"properties": {"name": "bar"}})
169
        response = self.ajaxPatch(newgroup,
170
                                  newprop,
171
                                  "application/json")
172
        self.assertEqual(response.status_code, 202)
173
        # TODO: Fetch graph and check that the property is really stored
174
175
    def testNodeGroupNodesChange(self):
176
        # TODO: Fixture should have a node group, instead of creating it here
177
        nodes1 = [fixt_simple['clientIdAndGate']]
178
        newgroup = json.dumps({'client_id': 999, 'nodeIds': nodes1})
179
        response = self.ajaxPost(self.baseUrl + '/graphs/%u/nodegroups/' % fixt_simple['pkDFD'],
180
                                 newgroup,
181
                                 'application/json')
182
        self.assertEqual(response.status_code, 201)
183
        newgroup = response['Location']
184
        # Try changing
185
        nodes2 = [
186
            fixt_simple['clientIdAndGate'],
187
            fixt_simple['clientIdBasicEvent']]
188
        newnodes = json.dumps({"nodeIds": nodes2})
189
        response = self.ajaxPatch(newgroup,
190
                                  newnodes,
191
                                  "application/json")
192
        self.assertEqual(response.status_code, 202)
193
        # Get complete graph and see if the node group is registered correctly
194
        url = self.baseUrl + '/graphs/%u' % fixt_simple['pkDFD']
195
        response = self.ajaxGet(url)
196
        self.assertEqual(response.status_code, 200)
197
        content = json.loads(response.content)
198
        for group in content['nodeGroups']:
199
            self.assertItemsEqual(group['nodeIds'], nodes2)
200
201
    def testEdgePropertyChange(self):
202
        newprop = json.dumps({"properties": {"name": "bar"}})
203
        response = self.ajaxPatch(self.baseUrl + '/graphs/%u/edges/%u' % (fixt_simple['pkDFD'], fixt_simple['clientIdEdgeDfd']),
204
                                  newprop,
205
                                  "application/json")
206
        self.assertEqual(response.status_code, 202)
207
        # TODO: Fetch graph and check that the property is really stored
208
209
    def testDeleteEdge(self):
210
        response = self.ajaxDelete(
211
            self.baseUrl + '/graphs/%u/edges/%u' % (fixt_simple['pkFaultTree'], fixt_simple['clientIdEdge']))
212
        self.assertEqual(response.status_code, 204)
213
214
    def testCreateEdge(self):
215
        initial_properties = {"name": "foo"}
216
        newedge = json.dumps(
217
            {'client_id': 4714,
218
                'source': fixt_simple['clientIdProcess'],
219
                'target': fixt_simple['clientIdStorage'],
220
                'properties': initial_properties
221
             }
222
        )
223
        # Only DFD edges support properties, so we use them here
224
        response = self.ajaxPost(self.baseUrl + '/graphs/%u/edges/' % fixt_simple['pkDFD'],
225
                                 newedge,
226
                                 'application/json')
227
        self.assertEqual(response.status_code, 201)
228
#        print response['Location']
229
#        newid = int(response['Location'].split('/')[-1])
230
#        newedge = Edge.objects.get(client_id=newid, deleted=False)
231
#        self.assertItemsEqual(initial_properties, newedge.get_properties())
232
233
    def testNotificationDismiss(self):
234
        # Create notification entry in the database
235
        u = User.objects.get(username='testadmin')
236
        n = Notification(title="Test notification")
237
        n.save()
238
        n.users.add(u)
239
        n.save()
240
        # Now check the dismiss call
241
        response = self.ajaxDelete(self.baseUrl + '/notification/%u/' % n.pk)
242
        self.assertEqual(response.status_code, 204)
243