Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change: 仕様変更に対応 #30

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions server/src/DB.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ def __init__(self):
db = client["db"]
self.__users = db["users"]
self.__device_keys = db["device_keys"]
self.__pre_pairings = db["pre_pairings"]
self.__pairings = db["pairings"]
self.__pre_passes = db["pre_passes"]
self.__now_passes = db["now_passes"]
Expand All @@ -25,11 +24,6 @@ def users(self):
def device_keys(self):
return self.__device_keys

# pre_pairings
@property
def pre_pairings(self):
return self.__pre_pairings

# pairings
@property
def pairings(self):
Expand Down
41 changes: 3 additions & 38 deletions server/src/Pairing/routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
db = DB()
users = db.users
device_keys = db.device_keys
pre_pairings = db.pre_pairings
pairings = db.pairings


Expand Down Expand Up @@ -51,51 +50,17 @@ def register_pairing():
if not device_key:
return jsonify({'error': 'Invalid public_key'}), 400

pre_pairings.insert_one({
'uid': user['uid'],
'public_key': public_key
})

return jsonify({'done': 'pre_pairing'}), 200


@PAIRING_BP.route('/check_pairing', methods=['POST'])
def check_pairing():
token = request.json['token']
if not token:
return jsonify({'error': 'Missing token'}), 400
private_key = request.json['private_key']
if not private_key:
return jsonify({'error': 'Missing private_key'}), 400

user = users.find_one({'token': token})
if not user:
return jsonify({'error': 'Invalid token'}), 400

device_key = device_keys.find_one({'private_key': private_key})
if not device_key:
return jsonify({'error': 'Invalid private_key'}), 400

pre_pairing = pre_pairings.find_one(
{'uid': user['uid'], 'public_key': device_key['public_key']})

if not pre_pairing:
return jsonify({'error': 'Invalid pairing'}), 400

pre_pairings.delete_many(
{'uid': user['uid'], 'public_key': device_key['public_key']})

pairings.delete_many({'uid': user['uid']})
pairings.delete_many({'public_key': device_key['public_key']})
pairings.delete_many({'private_key': private_key})
pairings.delete_many({'private_key': device_key['private_key']})

pairings.insert_one({
'uid': user['uid'],
'public_key': device_key['public_key'],
'private_key': private_key
'private_key': device_key['private_key']
})

return jsonify({'done': 'success'}), 200
return jsonify({'done': 'pairing'}), 200


@PAIRING_BP.route('/auth_check', methods=['POST'])
Expand Down
84 changes: 11 additions & 73 deletions server/tests/test_pairing.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,17 @@ def test_register_pairing_success(baseurl):
'public_key': public_key
})
assert res.status_code == 200
assert res.json()['done'] == 'pre_pairing'
assert res.json()['done'] == 'pairing'

client = MongoClient('localhost', 27017)
db = client['db']
pairings = db['pairings']
users = db['users']

user = users.find_one({'token': token})
pairing = pairings.find_one(
{'uid': user['uid'], 'public_key': public_key, 'private_key': private_key})
assert pairing


def test_register_pairing_missing_token(baseurl):
Expand Down Expand Up @@ -111,74 +121,6 @@ def test_register_pairing_invalid_public_key(baseurl):
assert res.json()['error'] == 'Invalid public_key'


def test_check_pairing_missing_token(baseurl):
global private_key
url = baseurl+'/pairing/check_pairing'
res = requests.post(url, json={
'token': '',
'private_key': private_key
})
assert res.status_code == 400
assert res.json()['error'] == 'Missing token'


def test_check_pairing_missing_private_key(baseurl):
global token
url = baseurl+'/pairing/check_pairing'
res = requests.post(url, json={
'token': token,
'private_key': ''
})
assert res.status_code == 400
assert res.json()['error'] == 'Missing private_key'


def test_check_pairing_invalid_token(baseurl):
global private_key
url = baseurl+'/pairing/check_pairing'
res = requests.post(url, json={
'token': 'invalidToken',
'private_key': private_key
})
assert res.status_code == 400
assert res.json()['error'] == 'Invalid token'


def test_check_pairing_invalid_private_key(baseurl):
global token
url = baseurl+'/pairing/check_pairing'
res = requests.post(url, json={
'token': token,
'private_key': 'invalidPrivateKey'
})
assert res.status_code == 400
assert res.json()['error'] == 'Invalid private_key'


def test_check_pairing_success(baseurl):
global token
global private_key
url = baseurl+'/pairing/check_pairing'
res = requests.post(url, json={
'token': token,
'private_key': private_key
})
assert res.status_code == 200
assert res.json()['done'] == 'success'


def test_check_pairing_invalid_pairing(baseurl):
global token
global private_key
url = baseurl+'/pairing/check_pairing'
res = requests.post(url, json={
'token': token,
'private_key': private_key
})
assert res.status_code == 400
assert res.json()['error'] == 'Invalid pairing'


def test_auth_check_success(baseurl):
global token
url = baseurl+'/pairing/auth_check'
Expand Down Expand Up @@ -220,10 +162,6 @@ def test_done(baseurl):

users = db['users']
user = users.find_one({'token': token})
pre_pairings = db['pre_pairings']
pre_pairings.delete_many({'uid': user['uid']})
assert not pre_pairings.find_one({'uid': user['uid']})

pairings = db['pairings']
pairings.delete_many({'uid': user['uid']})
assert not pairings.find_one({'uid': user['uid']})
Expand Down
25 changes: 2 additions & 23 deletions server/tests/test_streetpass.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def test_pairing_success(baseurl):
'public_key': public_key1
})
assert res.status_code == 200
assert res.json()['done'] == 'pre_pairing'
assert res.json()['done'] == 'pairing'

global token2
global private_key2
Expand All @@ -92,23 +92,7 @@ def test_pairing_success(baseurl):
'public_key': public_key2
})
assert res.status_code == 200
assert res.json()['done'] == 'pre_pairing'

url = baseurl+'/pairing/check_pairing'
res = requests.post(url, json={
'token': token1,
'private_key': private_key1
})
assert res.status_code == 200
assert res.json()['done'] == 'success'

url = baseurl+'/pairing/check_pairing'
res = requests.post(url, json={
'token': token2,
'private_key': private_key2
})
assert res.status_code == 200
assert res.json()['done'] == 'success'
assert res.json()['done'] == 'pairing'

url = baseurl+'/pairing/auth_check'
res = requests.post(url, json={
Expand Down Expand Up @@ -232,11 +216,6 @@ def test_done():
users = db['users']
user1 = users.find_one({'token': token1})
user2 = users.find_one({'token': token2})
pre_pairings = db['pre_pairings']
pre_pairings.delete_many({'uid': user1['uid']})
assert not pre_pairings.find_one({'uid': user1['uid']})
pre_pairings.delete_many({'uid': user2['uid']})
assert not pre_pairings.find_one({'uid': user2['uid']})

pairings = db['pairings']
pairings.delete_many({'uid': user1['uid']})
Expand Down