Skip to content

Commit 8a11367

Browse files
committed
updated model to not change id when deserializing
1 parent d6b52fe commit 8a11367

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-8
lines changed

Vagrantfile

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ Vagrant.configure(2) do |config|
4646
apt-get install -y git python-pip python-dev build-essential
4747
pip install --upgrade pip
4848
apt-get -y autoremove
49+
# Make vi look nice
50+
sudo -H -u ubuntu echo "colorscheme desert" > ~/.vimrc
4951
# Install app dependencies
5052
cd /vagrant
5153
sudo pip install -r requirements.txt
52-
# Make vi look nice
53-
sudo -H -u ubuntu echo "colorscheme desert" > ~/.vimrc
5454
SHELL
5555

5656
end

models.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ def deserialize(self, data):
7878
"""
7979
if not isinstance(data, dict):
8080
raise DataValidationError('Invalid pet: body of request contained bad or no data')
81-
if data.has_key('id'):
81+
# only update the id if it is zero
82+
# this prevents spoofing the id by changing the original id with the data
83+
if data.has_key('id') and self.id == 0:
8284
self.id = data['id']
8385
try:
8486
self.name = data['name']

tests/test_pets.py

+29-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1-
# Test cases can be run with:
2-
# nosetests
3-
# coverage report -m
4-
5-
""" Test cases for Pet Model """
1+
# Copyright 2016, 2017 John J. Rofrano. All Rights Reserved.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
"""
16+
Test cases for Pet Model
17+
18+
Test cases can be run with:
19+
nosetests
20+
coverage report -m
21+
"""
622

723
import unittest
824
from models import Pet, DataValidationError
@@ -82,6 +98,14 @@ def test_deserialize_a_pet(self):
8298
self.assertEqual(pet.id, 1)
8399
self.assertEqual(pet.name, "kitty")
84100
self.assertEqual(pet.category, "cat")
101+
# test with id passed into constructor
102+
pet = Pet(2)
103+
pet.deserialize(data)
104+
self.assertNotEqual(pet, None)
105+
self.assertEqual(pet.id, 2) # id should not change
106+
self.assertEqual(pet.name, "kitty")
107+
self.assertEqual(pet.category, "cat")
108+
85109

86110
def test_deserialize_with_no_name(self):
87111
""" Deserialize a Pet without a name """

0 commit comments

Comments
 (0)