Completed
Push — master ( 8a1e4c...0fd553 )
by
unknown
26s
created

FunctionalTests.test_get_nearest_address_mock()   A

Complexity

Conditions 1

Size

Total Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 9
rs 9.6666
1
# -*- coding: utf-8 -*-
2
import os
3
import unittest
4
import json
5
6
from paste.deploy import appconfig
7
from webtest import TestApp
8
from pyramid import testing
9
import responses
10
11
try:
12
    from testdata import test_json_outside_flanders, test_json_intersects_flanders
13
except:
14
    from tests.testdata import test_json_outside_flanders, test_json_intersects_flanders
15
16
try:
17
    from __init__ import text_, mock_geozoekdiensten_response, mock_geozoekdiensten_get_gemeente_response, get_provincie_results, get_gemeente_results
18
except:
19
    from tests import text_, mock_geozoekdiensten_response, mock_geozoekdiensten_get_gemeente_response, get_provincie_results, get_gemeente_results
20
21
from oe_geoutils import main
22
23
from oe_geoutils.views.exceptions import internal_server_error
24
import pytest
25
26
integration = pytest.mark.skipif(
27
    not pytest.config.getoption("--integration"),
28
    reason="need --integration option to run"
29
)
30
31
here = os.path.dirname(__file__)
32
33
test_geom = json.dumps({
34
            "type": "MultiPolygon",
35
            "coordinates": [[[[172933.6922879719058983, 174851.14960918109863997],
36
                              [172930.21180502674542367, 174832.7836931711062789],
37
                              [172920.64762709615752101, 174848.13247794657945633],
38
                              [172933.6922879719058983, 174851.14960918109863997]]]],
39
            "crs": {
40
                "type": "name",
41
                "properties": {
42
                    "name": "urn:ogc:def:crs:EPSG::31370"
43
                }
44
            }
45
        })
46
47
48
class FunctionalTests(unittest.TestCase):
49
    def _get_default_headers(self):
50
        return {'Accept': 'application/json'}
51
52
    @classmethod
53
    def setUpClass(cls):
54
        cls.settings = appconfig('config:' + os.path.join(here, 'test.ini'))
55
56
    def setUp(self):
57
        self.app = main({}, **self.settings)
58
        self.testapp = TestApp(self.app)
59
        responses.add(responses.POST, "https://test-geo.onroerenderfgoed.be/zoekdiensten/administratievegrenzen")
60
61
    def tearDown(self):
62
        self.testapp.reset()
63
64
    @integration
65
    def test_get_nearest_address(self):
66
        res = self.testapp.post('/nearest_address', test_geom)
67
        self.assertEqual('200 OK', res.status)
68
69
    @responses.activate
70
    def test_get_nearest_address_mock(self):
71
        responses.add(
72
            responses.GET,
73
            'https://loc.geopunt.be/geolocation/Location',
74
            body='{"LocationResult":[{"ID":201984,"FormattedAddress":"Fonteinstraat 75, 3000 Leuven","Location":{"Lat_WGS84":50.883485330273977,"Lon_WGS84":4.6941590167952487,"X_Lambert72":172899.0,"Y_Lambert72":174842.0},"LocationType":"crab_huisnummer_afgeleidVanGebouw","BoundingBox":{"LowerLeft":{"Lat_WGS84":50.883485330273977,"Lon_WGS84":4.6941590167952487,"X_Lambert72":172899.0,"Y_Lambert72":174842.0},"UpperRight":{"Lat_WGS84":50.883485330273977,"Lon_WGS84":4.6941590167952487,"X_Lambert72":172899.0,"Y_Lambert72":174842.0}}}]}',
75
            status=200)
76
        res = self.testapp.post('/nearest_address', test_geom)
77
        self.assertEqual('200 OK', res.status)
78
79
    def test_get_nearest_address_outside_Flanders(self):
80
        res = self.testapp.post('/nearest_address', json.dumps(test_json_outside_flanders), expect_errors=True)
81
        self.assertEqual('400 Bad Request', res.status)
82
83
    @integration
84
    def test_get_nearest_address_not_found(self):
85
        res = self.testapp.post('/nearest_address', json.dumps(test_json_intersects_flanders), expect_errors=True)
86
        self.assertEqual('200 OK', res.status)
87
88
    @responses.activate
89
    def test_get_nearest_address_not_found_mock(self):
90
        responses.add(
91
            responses.GET,
92
            'https://loc.geopunt.be/geolocation/Location',
93
            body='{"LocationResult":[{"ID":1031530,"FormattedAddress":"Linkebeekstraat 35, 1180 Ukkel","Location":{"Lat_WGS84":50.779580783177835,"Lon_WGS84":4.327986356112306,"X_Lambert72":147125.0,"Y_Lambert72":163234.0},"LocationType":"urbis_huisnummer","BoundingBox":{"LowerLeft":{"Lat_WGS84":50.779580783177835,"Lon_WGS84":4.327986356112306,"X_Lambert72":147125.0,"Y_Lambert72":163234.0},"UpperRight":{"Lat_WGS84":50.779580783177835,"Lon_WGS84":4.327986356112306,"X_Lambert72":147125.0,"Y_Lambert72":163234.0}}}]}',
94
            status=200
95
        )
96
        res = self.testapp.post('/nearest_address', json.dumps(test_json_intersects_flanders), expect_errors=True)
97
        self.assertEqual('200 OK', res.status)
98
99
    def test_check_in_flanders(self):
100
        res = self.testapp.post('/check_in_flanders', test_geom)
101
        self.assertEqual('200 OK', res.status)
102
103
    def test_check_within_flanders(self):
104
        res = self.testapp.post('/check_within_flanders', test_geom)
105
        self.assertEqual('200 OK', res.status)
106
107
    def test_check_in_flanders_no_json_body(self):
108
        res = self.testapp.post('/check_in_flanders', expect_errors=True)
109
        self.assertEqual('400 Bad Request', res.status)
110
111
    def test_check_in_flanders_validation_failure(self):
112
        res = self.testapp.post('/check_in_flanders', '{}', expect_errors=True)
113
        self.assertEqual('400 Bad Request', res.status)
114 View Code Duplication
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
115
    def test_check_in_flanders_invalid_url(self):
116
        res = self.testapp.post('/test', '{}', expect_errors=True)
117
        self.assertEqual('404 Not Found', res.status)
118
119
    def test_internal_server_error(self):
120
        a = Exception()
121
        internal_server_error(a, testing.DummyRequest())
122
123
    @responses.activate
124
    def test_gemeente(self):
125
        res = self.testapp.post('/gemeente', test_geom)
126
        self.assertEqual('200 OK', res.status)
127
        print(res.text)
128
129
    @responses.activate
130
    def test_gemeente(self):
131
        responses.add(responses.POST, 'http://geozoekdienst.en', body=json.dumps(get_gemeente_results))
132
        res = self.testapp.post('/gemeente', test_geom)
133
        self.assertEqual('200 OK', res.status)
134
        print(res.text)
135
136
    @responses.activate
137
    def test_provincie(self):
138
        responses.add(responses.POST, 'http://geozoekdienst.en', body=json.dumps(get_provincie_results))
139
        res = self.testapp.post('/provincie', test_geom)
140
        self.assertEqual('200 OK', res.status)
141
        print(res.text)
142
143
    @responses.activate
144
    def test_check_erfgoedgemeente(self):
145 View Code Duplication
        contour = {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
146
            "coordinates": [[[[172933.6922879719, 174851.1496091811], [172930.21180502675, 174832.7836931711],
147
                              [172920.64762709616, 174848.13247794658], [172933.6922879719, 174851.1496091811]]]],
148
            "type": "MultiPolygon", "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::31370"}}
149
        }
150
        responses.add(
151
            responses.POST,
152
            'http://geozoekdienst.en',
153
            body='[{"naam": "Leuven", "type": "gemeente", "id": "24062"}]', status=200)
154
        res = self.testapp.post('/check_in_erfgoedgemeente', params=json.dumps(contour),
155
                                headers={'Accept': 'application/json', 'Content-Type': 'application/json'})
156
        self.assertIn('ok', res.json["status"])
157
158
    @responses.activate
159
    def test_check_erfgoedgemeente_full_overlap(self):
160
        responses.add(
161
            responses.POST,
162
            'http://geozoekdienst.en',
163
            body='[{"naam": "Koksijde", "type": "gemeente", "id": "38014"}]', status=200)
164
        contour = {
165
            "coordinates": [[[[172933.6922879719, 174851.1496091811], [172930.21180502675, 174832.7836931711],
166
                              [172920.64762709616, 174848.13247794658], [172933.6922879719, 174851.1496091811]]]],
167
            "type": "MultiPolygon", "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::31370"}}
168
        }
169
        res = self.testapp.post('/check_in_erfgoedgemeente', params=json.dumps(contour),
170
                                headers={'Accept': 'application/json', 'Content-Type': 'application/json'})
171
        self.assertIn('error', res.json["status"])
172
        self.assertIn('Gelieve de melding in te dienen bij deze gemeente', res.json["message"])
173
174
    @responses.activate
175
    def test_check_erfgoedgemeente_partial_overlap(self):
176
        responses.add(
177
            responses.POST,
178
            'http://geozoekdienst.en',
179
            body='[{"naam": "Koksijde", "type": "gemeente", "id": "38014"}, '
180
                 '{"naam": "Nieuwpoort", "type": "gemeente", "id": "38016"}]', status=200)
181
        contour = {
182
            "coordinates": [[[[172933.6922879719, 174851.1496091811], [172930.21180502675, 174832.7836931711],
183
                              [172920.64762709616, 174848.13247794658], [172933.6922879719, 174851.1496091811]]]],
184
            "type": "MultiPolygon", "crs": {"type": "name", "properties": {"name": "urn:ogc:def:crs:EPSG::31370"}}
185
        }
186
        res = self.testapp.post('/check_in_erfgoedgemeente', params=json.dumps(contour),
187
                                headers={'Accept': 'application/json', 'Content-Type': 'application/json'})
188
        self.assertIn('warn', res.json["status"])
189
        self.assertIn('Gelieve de melding vooronderzoek eveneens in te dienen bij deze gemeente', res.json['message'])
190
191
192
193