Skip to content

Commit c84e0db

Browse files
author
s0561440
committed
tests passing
1 parent bb4c947 commit c84e0db

File tree

5 files changed

+71
-36
lines changed

5 files changed

+71
-36
lines changed

.travis.yml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ python:
55
services:
66
- docker
77

8-
before_script: pip install docker-compose
9-
108
install:
119
- docker-compose build && docker-compose up -d
1210

11+
before_script:
12+
- pip install requests docker
13+
1314
script:
1415
- pytest tests/
1516

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# AccessPointFingerprintAPI
2+
3+
[![Build Status](https://travis-ci.com/FalcoSuessgott/AccessPointFingerprintAPI.svg?branch=master)](https://travis-ci.com/FalcoSuessgott/AccessPointFingerprintAPI)
4+
25
This Project stores Access Points Fingerprints from mobile devices (e.g smartphone, esp8266, ..) and its geolocation.
36
If requested, the API responds with the corresponding geolocation for a specific fingerprint.
47

@@ -38,7 +41,9 @@ It is adapted to the [BVGDetection](https://github.com/OpenHistoricalDataMap/BVG
3841
```
3942
git clone https://github.com/FalcoSuessgott/AccessPointFingerprintAPI
4043
cd AccessPointFingerprintAPI
41-
docker-compuse up
44+
docker-compose build && docker-compuse up
45+
# running tests
46+
pytest tests/
4247
```
4348

4449
# Project Setup

api/app.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ def get_fingerprint(fingerprint_id):
183183
'additionalInfo': fp['additionalInfo'],
184184
'fingerprint': fp['fingerprint']
185185
}]
186-
return jsonify(status=200, data=data)
186+
return jsonify(data=data), 200
187187
else:
188-
return jsonify(status=404, message="No fingerprint were found with this ID.")
188+
return jsonify(message="No fingerprint were found with this ID."), 404
189189

190190

191191
@application.route('/fingerprint', methods=["POST"])
@@ -198,9 +198,9 @@ def post_fingerprint():
198198
fp = request.get_json()
199199
validate(instance=fp, schema=FINGERPRINT_SCHEMA)
200200
db.fingerprint.insert_one(fp)
201-
return jsonify(status=201, message='Fingerprint saved successfully!')
201+
return jsonify(message='Fingerprint saved successfully!'), 201
202202
except Exception as e:
203-
return Response('Exception while storing fingerprint: %s' % e, status=400)
203+
return jsonify(message='Fingerprint could not be saved', error=str(e)), 400
204204

205205

206206
@application.route('/fingerprint/<string:fingerprint_id>', methods=["PUT"])
@@ -216,11 +216,11 @@ def put_fingerprint(fingerprint_id):
216216
request_fp = request.get_json()
217217
validate(instance=request_fp, schema=FINGERPRINT_SCHEMA)
218218
db.fingerprint.find_one_and_update({'id': fingerprint_id}, {'$set': request_fp})
219-
return jsonify(status=200, message='Fingerprint updated successfully!')
219+
return jsonify(message='Fingerprint updated successfully!'), 201
220220
except Exception as e:
221-
return jsonify(status=400, message='Exception while updating fingerprint: %s' % e)
221+
return jsonify(message='Fingerprint could not be updated', error=str(e)), 400
222222
else:
223-
return jsonify(status=404, message="No fingerprint were found with this ID.")
223+
return jsonify(message="No fingerprint were found with this ID."), 404
224224

225225

226226
@application.route('/fingerprint/<string:fingerprint_id>', methods=["DELETE"])
@@ -229,10 +229,13 @@ def delete_fingerprint(fingerprint_id):
229229
deletes a fingerprint
230230
"""
231231

232-
if db.fingerprint.delete_one({'id': fingerprint_id}):
233-
return jsonify(status=201, message='Fingerprint deletes successfully!')
232+
fp = db.fingerprint.find_one({'id': fingerprint_id})
233+
234+
if fp:
235+
db.fingerprint.delete_one({'id': fingerprint_id})
236+
return jsonify(message='Fingerprint deletes successfully!'), 201
234237
else:
235-
return jsonify(status=404, message="No fingerprint were found with this ID.")
238+
return jsonify(message="No fingerprint were found with this ID."), 404
236239

237240

238241
@application.route('/localize', methods=["GET"])

mongo-init.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
db.auth('test', 'test123');
2+
db = db.getSiblingDB('testdb');
3+
db.createUser(
4+
{
5+
user: "flask",
6+
pwd: "flask123",
7+
roles: [
8+
{
9+
role: "readWrite",
10+
db: "testdb"
11+
}
12+
]
13+
}
14+
);

tests/test_fingerprint.py

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,52 @@
11
import requests
22
import json
33
import codecs
4+
import docker
45

5-
IP = "192.168.178.70"
6+
7+
def get_ip_from_container(container_name, network):
8+
"""
9+
returns the ip of docker container
10+
"""
11+
12+
client = docker.DockerClient()
13+
container = client.containers.get(container_name)
14+
return container.attrs['NetworkSettings']['Networks'][network]['IPAddress']
15+
16+
17+
API_IP = get_ip_from_container("api", "accesspointfingerprintapi_frontend")
618
PORT = 5000
719
ENDPOINT = "fingerprint"
8-
URI = "http://" + IP + ":" + str(PORT) + "/" + ENDPOINT
20+
URI = "http://" + API_IP + ":" + str(PORT) + "/" + ENDPOINT
921
HEADERS = {'Content-Type': 'application/json'}
1022

1123

1224
###########
13-
# GET #
25+
# POST #
1426
###########
1527

1628

17-
def test_get_fingerprint_should_return_200():
18-
assert requests.get(URI + "/test").status_code == 200
29+
def test_post_fingerprint_should_return_201():
30+
with codecs.open('tests/test_fp.json', 'r', 'utf-8-sig') as json_file:
31+
data = json.load(json_file)
1932

33+
assert requests.post(URI, json=data, headers=HEADERS).status_code == 201
2034

21-
def test_get_fingerprint_should_return_404():
22-
assert requests.get(URI + "/invalid").status_code == 404
2335

36+
def test_post_fingerprint_should_return_400():
37+
assert requests.post(URI, json={'some': 'Data'}, headers=HEADERS).status_code == 400
2438

2539
###########
26-
# POST #
40+
# GET #
2741
###########
2842

2943

30-
def test_post_fingerprint_should_return_201():
31-
with codecs.open('test_fp.json', 'r', 'utf-8-sig') as json_file:
32-
data = json.load(json_file)
33-
34-
assert requests.post(URI, data=json.dumps(data), headers=HEADERS).status_code == 201
44+
def test_get_fingerprint_should_return_200():
45+
assert requests.get(URI + "/test_id").status_code == 200
3546

3647

37-
def test_post_fingerprint_should_return_400():
38-
assert requests.post(URI, data=json.dumps({'some' : 'Data'}), headers=HEADERS).status_code == 400
48+
def test_get_fingerprint_should_return_404():
49+
assert requests.get(URI + "/invalid").status_code == 404
3950

4051

4152
###########
@@ -44,27 +55,28 @@ def test_post_fingerprint_should_return_400():
4455

4556

4657
def test_put_fingerprint_should_return_400():
47-
assert requests.post(URI + "/test_id", data=json.dumps({'some' : 'Data'}), headers=HEADERS).status_code == 400
58+
assert requests.put(URI + "/test_id", json={'some': 'Data'}, headers=HEADERS).status_code == 400
4859

4960

5061
def test_put_fingerprint_should_return_404():
51-
assert requests.post(URI + "/invalid", data=json.dumps({'some' : 'Data'}), headers=HEADERS).status_code == 400
62+
assert requests.put(URI + "/invalid", json={'some': 'Data'}, headers=HEADERS).status_code == 404
5263

5364

54-
def test_put_fingerprint_should_return_200():
55-
with codecs.open('test_fp.json', 'r', 'utf-8-sig') as json_file:
65+
def test_put_fingerprint_should_return_201():
66+
with codecs.open('tests/test_fp.json', 'r', 'utf-8-sig') as json_file:
5667
data = json.load(json_file)
5768

58-
assert requests.post(URI + "/test_id", data=json.dumps(data), headers=HEADERS).status_code == 200
69+
assert requests.put(URI + "/test_id", json=data, headers=HEADERS).status_code == 201
70+
5971

6072
#############
6173
# DELETE #
6274
#############
6375

6476

65-
def test_delete_fingerprint_should_return_200():
66-
assert requests.post(URI + "/test_id", data=json.dumps({'some' : 'Data'}), headers=HEADERS).status_code == 200
77+
def test_delete_fingerprint_should_return_201():
78+
assert requests.delete(URI + "/test_id").status_code == 201
6779

6880

6981
def test_delete_fingerprint_should_return_404():
70-
assert requests.post(URI + "/test_id", data=json.dumps({'some' : 'Data'}), headers=HEADERS).status_code == 404
82+
assert requests.delete(URI + "/invalid").status_code == 404

0 commit comments

Comments
 (0)