ore.tests.common   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 154
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 92
dl 0
loc 154
rs 10
c 0
b 0
f 0

11 Methods

Rating   Name   Duplication   Size   Complexity  
A OreTestHelpers.requestJob() 0 31 2
A OreTestHelpers.ajaxPatch() 0 6 1
A OreTestHelpers.postWithAPIKey() 0 3 1
A OreTestHelpers.ajaxPost() 0 6 1
A OreTestHelpers.ajaxDelete() 0 2 1
A OreTestHelpers.setUpLogin() 0 4 1
A OreTestHelpers.ajaxGet() 0 2 1
A OreTestHelpers.post() 0 2 1
A OreTestHelpers.getWithAPIKey() 0 3 1
A OreTestHelpers.setUpAnonymous() 0 3 1
A OreTestHelpers.get() 0 2 1
1
'''
2
    This is the test suite.
3
4
    TODO: Several test would look better if the model is checked afterwards for the changes being applied
5
        (e.g. node relocation). Since we use the LiveServerTestCase base, this is not possible, since
6
        database modifications are not commited at all. Explicit comitting did not help ...
7
'''
8
9
import json
10
import urlparse
11
12
from django.test import LiveServerTestCase, TestCase
13
from django.test.client import Client
14
15
16
# This disables all the debug output from the server, e.g. Latex rendering nodes etc.
17
# import logging
18
# logging.disable(logging.CRITICAL)
19
20
# The fixtures used in different test classes
21
fixt_analysis = {
22
    'files': ['analysis.json', 'testuser.json'],
23
    'graphs': {7: 'faulttree', 8: 'faulttree'},
24
    'results': {7: 'results/rate_tree.xml', 8: 'results/prdc_tree.xml'},
25
    'rate_faulttree': 7,                # Graph PK
26
    'prdc_fuzztree': 8,                # Graph PK
27
    'prdc_configurations': 8,           # Decomposition number
28
    'prdc_peaks': [0.31482, 0.12796, 0.25103, 0.04677, 0.36558, 0.19255, 0.30651, 0.11738]
29
}
30
31
fixt_simple = {
32
    'files': ['simple.json', 'testuser.json'],
33
    'graphs': {1: 'faulttree', 2: 'fuzztree', 3: 'rbd', 7: 'dfd'},
34
    'pkProject': 1,
35
    'pkFaultTree': 1,
36
    'pkDFD': 7,
37
    'clientIdEdge': 4,
38
    'clientIdAndGate': 1,
39
    'clientIdBasicEvent': 2,
40
    'clientIdProcess': 1,
41
    'clientIdStorage': 2,
42
    'clientIdExternal': 3,
43
    'clientIdEdgeDfd': 5,
44
}
45
46
fixt_mincut = {
47
    'files': ['mincut1.json', 'testuser.json'],
48
    'mincut_faulttree': 1,
49
    'mincut_numcuts': 3
50
}
51
52
fixt_unicode = {
53
    'files': ['unicode.json', 'testuser.json'],
54
    'graphs': {1: 'faulttree'},
55
    'pkProject': 1,
56
    'pkFaultTree': 1
57
58
}
59
60
61
class OreTestHelpers():
62
63
    """
64
        The base class for all test cases that rely on a reachable web server during testing.
65
66
        This is need when the backend daemon needs to call back, or if some HTTP redirection target
67
        is needed.
68
69
        Mainly provides helper functions for deal with auth stuff.
70
    """
71
72
    def setUpAnonymous(self):
73
        ''' If the test case wants to have a anonymous login session, it should call this function in setUp().'''
74
        self.c = Client()
75
76
    def setUpLogin(self):
77
        ''' If the test case wants to have a functional login session, it should call this function in setUp().'''
78
        self.c = Client()
79
        self.c.login(username='testadmin', password='testadmin')
80
81
    def get(self, url):
82
        return self.c.get(url)
83
84
    def post(self, url, data):
85
        return self.c.post(url, data)
86
87
    def getWithAPIKey(self, url):
88
        return self.c.get(
89
            url, **{'HTTP_AUTHORIZATION': 'ApiKey f1cc367bc09fc95720e6c8a4225ae2b912fff91b'})
90
91
    def postWithAPIKey(self, url, data, content_type):
92
        return self.c.post(url, data, content_type,
93
                           **{'HTTP_AUTHORIZATION': 'ApiKey f1cc367bc09fc95720e6c8a4225ae2b912fff91b'})
94
95
    def ajaxGet(self, url):
96
        return self.c.get(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
97
98
    def ajaxPost(self, url, data, content_type):
99
        """
100
        :rtype : django.http.response.HttpResponse
101
        """
102
        return self.c.post(
103
            url, data, content_type, **{'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'})
104
105
    def ajaxPatch(self, url, data, content_type):
106
        """
107
        :rtype : django.http.response.HttpResponse
108
        """
109
        return self.c.patch(
110
            url, data, content_type, **{'HTTP_X_REQUESTED_WITH': 'XMLHttpRequest'})
111
112
    def ajaxDelete(self, url):
113
        return self.c.delete(url, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
114
115
    def requestJob(self, base_url, graph, kind):
116
        """
117
            Helper function for requesting a job. Waits for the result and returns its URL.
118
        """
119
        newjob = json.dumps({'kind': kind})
120
        response = self.ajaxPost(
121
            base_url +
122
            '/graphs/%u/jobs/' %
123
            graph,
124
            newjob,
125
            'application/json')
126
        self.assertNotEqual(
127
            response.status_code,
128
            500)  # the backend daemon is not started
129
        self.assertEqual(
130
            response.status_code,
131
            201)  # test if we got a created job
132
        assert ('Location' in response)
133
        # hardcoded host in docker test suite runs
134
        parsed = list(urlparse.urlparse(response['Location']))
135
        parsed[1] = "front:8000"
136
        jobUrl = urlparse.urlunparse(parsed)
137
        code = 202
138
        assert (not jobUrl.endswith('jobs/'))
139
        print "Waiting for result from " + jobUrl,
140
        while (code == 202):
141
            response = self.ajaxGet(jobUrl)
142
            code = response.status_code
143
        self.assertEqual(response.status_code, 200)
144
        assert ('Location' in response)
145
        return response
146
147
148
class OreLiveServerTestCase(LiveServerTestCase, OreTestHelpers):
149
    pass
150
151
152
class OreTestCase(TestCase, OreTestHelpers):
153
    pass
154