def setUp(self): super(BucketlistResourceTest, self).setUp() self.user = {"username": self.saved_user.username, "password": "******"} self.other_user = { "username": self.saved_user_2.username, "password": "******" } # create a bucketlist for tyrion current_user = User.query.filter_by( username=self.saved_user.username).first() self.example_bucketlist_one = Bucketlist( "bucketlist_one", "this is an example bucketlist", current_user.id) db.session.add(self.example_bucketlist_one) db.session.commit() # create a second bucketlist for tyrion self.example_bucketlist_two = Bucketlist( "bucketlist_two", "this is an another bucketlist", current_user.id) db.session.add(self.example_bucketlist_two) db.session.commit() self.bucketlist_one_id = Bucketlist.query.filter_by( name="bucketlist_one").first().id self.response = self.client.post('/api/v1/auth/login', data=json.dumps(self.user), headers=self.headers) self.response_content = json.loads(self.response.data) self.headers['Authorization'] = 'JWT {}'.format( self.response_content['access_token'])
def bucketlists(): if request.method == "POST": name = str(request.data.get('name', '')) if name: bucketlist = Bucketlist(name=name) bucketlist.save() response = jsonify({ 'id': bucketlist.id, 'name': bucketlist.name, 'date_created': bucketlist.date_created, 'date_modified': bucketlist.date_modified }) response.status_code = 201 return response else: # GET bucketlists = Bucketlist.get_all() results = [] for bucketlist in bucketlists: obj = { 'id': bucketlist.id, 'name': bucketlist.name, 'date_created': bucketlist.date_created, 'date_modified': bucketlist.date_modified } results.append(obj) response = jsonify(results) response.status_code = 200 return response
def setUp(self): """ Create test database and set up test client """ self.app = app.test_client() db.create_all() user = User(username="******", password="******") bucketlist1 = Bucketlist(title="Knowledge Goals", description="Things to learn", created_by=1) bucketlist2 = Bucketlist(title="Adventures", description="Awesome adventures to go on", created_by=1) item1 = Item(title="Learn to Cook", description="Cook at least 10 different meals", created_by=1, bucketlist_id=1) item2 = Item(title="Swim with Dolphins", description="Go swimming with dolphins in Watamu", created_by=1, bucketlist_id=2) db.session.add(user) db.session.add(bucketlist1) db.session.add(bucketlist2) db.session.add(item1) db.session.add(item2) db.session.commit()
def bucketlists(): if request.method == 'POST': name=str(request.data.get('name', '')) if name: bucketlist = Bucketlist(name=name) bucketlist.save() response = jsonify({ 'id': bucketlist.id, 'name': bucketlist.date_created, 'date_created': bucketlist.date_created, 'date_modiified': bucketlist.date_modified }) response.status_code = 201 return response else: bucketlists = Bucketlist.get_all() results = [] for bucketlist in bucketlists: obj = { 'id': bucketlist.id, 'name': bucketlist.name, 'date_created': bucketlist.date_created, 'date_modified':bucketlist.date_modified } results.append(obj) response = jsonify(results) response.status_code = 200 return response
def bucketlists(): """method for retrieving and creating bucket lists.""" access_token = request.headers.get("Authorization") if access_token: user_id = User.decode_token(access_token) if not isinstance(user_id, str): if request.method == 'POST': name = str(request.data.get('name', '')) u_id = user_id if name: bucketlist = Bucketlist(name=name, user_id=u_id) bucketlist.save() response = jsonify({ 'id': bucketlist.id, 'name': bucketlist.name }) response.status_code = 201 return response else: bucket_lists = Bucketlist.get_all() results = [] for bucketlist in bucket_lists: obj = { 'id': bucketlist.id, 'name': bucketlist.name, } results.append(obj) response = jsonify(results) response.status_code = 200 return response response = {"message": "Please login first."} response.status_code = 403 return jsonify(response)
def setUp(self): from app.models import Bucketlist self.app = create_app(config_name='testing') self.client = self.app.test_client with self.app.app_context(): db.create_all() bucketlist = Bucketlist(name='Go to Delaware') bucketlist.save() self.bucketlist_id = bucketlist.id
def setUp(self): """sets up moc data for tests""" self.app = app.test_client() app.config.from_object(config.TestingConfig) db.create_all() db.session.add(User(name='testuser', email='*****@*****.**', password=generate_password_hash( 'testpass', method='sha256'))) db.session.add(Bucketlist(name='testbucket', owner_id=1)) db.session.add(Bucketlist(name='testbucket2', owner_id=1)) db.session.add(BucketlistItem(description='itemname', bucketlist_id=2)) db.session.commit()
def bucketlists(): # Get the access token from the header auth_header = request.headers.get('Authorization') access_token = auth_header.split(" ")[1] if access_token: # Attempt to decode the token and get the User ID user_id = User.decode_token(access_token) if not isinstance(user_id, str): # Go ahead and handle the request, the user is authenticated if request.method == "POST": bucket_name = str(request.data.get('bucket_name', '')) belongs_to = int() if not bucket_name: return { "message": "Please add a goal to your bucketlist" }, 404 if bucket_name: bucketlist = Bucketlist(bucket_name=bucket_name) bucketlist.belongs_to = user_id bucketlist.save() response = jsonify({ 'id': bucketlist.id, 'bucket_name': bucketlist.bucket_name, 'date_created': bucketlist.date_created, 'date_modified': bucketlist.date_modified, 'belongs_to': user_id }) return make_response(response), 201 else: # GET all buckets bucketlists = Bucketlist.query.filter_by(belongs_to=user_id) results = [] for bucketlist in bucketlists: obj = { 'id': bucketlist.id, 'bucket_name': bucketlist.bucket_name, 'date_created': bucketlist.date_created, 'date_modified': bucketlist.date_modified, 'belongs_to': bucketlist.belongs_to } results.append(obj) return make_response(jsonify(results)), 200 else: # user is not legit, so the payload is an error message message = user_id response = { 'message': message } return make_response(jsonify(response)), 401
def post(self): title = request.form.get('title') description = request.form.get('description') fulfilled = False Bucketlist.create( title=title, description=description, fulfilled=fulfilled ) print('Title: {}\nDescription: {}'.format(title, description)) print(request.form) res = {'Status': 'OK'} return jsonify(res)
def bucketlists(): # Get the access token from the header auth_header = request.headers.get('Authorization') print("Auth header is ",auth_header) access_token = auth_header.split(" ")[1] if access_token: # Attempt to decode the token and get the User ID user_id = User.decode_token(access_token) print("user id is ",user_id) if not isinstance(user_id, str): # Go ahead and handle the request, the user is authenticated if request.method == "POST": content=request.json name=content["name"] # name = str(request.data.get('name', '')) if name: bucketlist = Bucketlist(name=name, created_by=user_id) bucketlist.save() response = jsonify({ 'id': bucketlist.id, 'name': bucketlist.name, 'date_created': bucketlist.date_created, 'date_modified': bucketlist.date_modified, 'created_by': user_id }) return make_response(response), 201 else: # GET all the bucketlists created by this user bucketlists = Bucketlist.query.filter_by(created_by=user_id) results = [] for bucketlist in bucketlists: obj = { 'id': bucketlist.id, 'name': bucketlist.name, 'date_created': bucketlist.date_created, 'date_modified': bucketlist.date_modified, 'created_by': bucketlist.created_by } results.append(obj) return make_response(jsonify(results)), 200 else: # user is not legit, so the payload is an error message message = user_id response = { 'message': message } return make_response(jsonify(response)), 401
def bucketlists_post(): ''' Function for adding a users new bucketlist ''' auth_header = request.headers.get('Authorization') access_token = auth_header if access_token: user_id = User.decode_token(access_token) if not isinstance(user_id, str): user = User.query.filter_by(id=user_id).first() user_buckelists_list = [] user_buckelists = Bucketlist.query.filter_by( user_id=user_id).all() for user_buckelist in user_buckelists: user_buckelists_list.append(user_buckelist) user_bucketlist_id = len(user_buckelists_list) + 1 if request.method == "POST": name = str(request.data.get('name', '')) if name: items = Bucketlist.query.filter_by( user_id=user_id).all() items_list = [] for item in items: items_list.append(item.name) if name in items_list: response = jsonify( {'message': 'The bucketlist already exists'}) response.status_code = 400 return response else: bucketlist = Bucketlist( bucketlist_id=user_bucketlist_id, name=name, user_id=user.id) bucketlist.save() response = jsonify({ 'id': bucketlist.id, 'bucketlist id': bucketlist.bucketlist_id, 'name': bucketlist.name, 'date_created': bucketlist.date_created, 'date_modified': bucketlist.date_modified, }) response.status_code = 201 return response else: response = jsonify({'message': 'Invalid token'}) response.status_code = 401 return response
def create_bucketlists(): name = str(request.data.get("name","")) bucketlist = Bucketlist(name=name) bucketlist.save() response = jsonify({ "id": bucketlist.id, "name": bucketlist.name, "date_created": bucketlist.date_created, "date_modified": bucketlist.date_modified }) response.status_code = 201 return response
def bucketlists(): name = str(request.data.get('name', '')) if name: bucketlist = Bucketlist(name=name) bucketlist.save() response = jsonify({ 'id': bucketlist.id, 'name': bucketlist.name, 'date_created': bucketlist.date_created, 'date_modified': bucketlist.date_modified }) response.status_code = 201 return response
class ModelTestCase(TestCase): """Defines the test suites for the bucket list model.""" def setUp(self): """ test client and other test variables.""" user = User.objects.create(username="******") self.name = "Write world class code" self.bucketlist = Bucketlist(name=self.name, owner=user) def test_model_can_create_a_bucketlist(self): """ Test bucketlist model can create a bucktlist.""" old_count = Bucketlist.objects.count() self.bucketlist.save() new_count = Bucketlist.objects.count() self.assertNotEqual(old_count,new_count)
def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.user = User(email="*****@*****.**", password="******") self.user.save() self.bucket_item = Bucketlist(name="Bucket List 1", user_id=self.user.id) self.bucket_item.save() self.item = Item(name="Needs of bucket items", bucketlist_id=self.bucket_item.id) self.item.save()
def setUp(self): # setup the app and push app context: self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() # setup the db: db.drop_all() db.create_all() # create test datas for the users, bucketlists, and items: user = User( username='******', email='*****@*****.**', password='******' ) user.save() self.user = User(email="*****@*****.**", password="******", username="******") self.user.save() self.bucket_item = Bucketlist(name="Bucket List 1", user_id=self.user.id) self.bucket_item.save() self.item = Item(name="Item 1", bucketlist_id=self.bucket_item.id) self.item.save() self.user2 = User(email="*****@*****.**", password="******", username="******") self.user2.save() self.bucket_item2 = Bucketlist(name="Bucket List 2", user_id=self.user2.id) self.bucket_item2.save() self.item2 = Item(name="Item 1", bucketlist_id=self.bucket_item2.id) self.item2.save() bucket_item = Bucketlist(name="Bucket List", user_id=user.id) bucket_item.save() item = Item(name="Item 1", bucketlist_id=bucket_item.id) item.save() self.client = self.app.test_client() #create default login login_details = { 'email': '*****@*****.**', 'password': '******'} response = self.client.post( url_for('api.login'), headers=self.get_api_headers(), data=json.dumps(login_details) ) self.token = json.loads(response.data)['token']
def post(self, auth_data): """ method used to create a bucketlist """ title = request.get_json().get('title') intro = request.get_json().get('intro') bucketlist_ = Bucketlist.query.filter_by(title=title).first() if intro.strip() and title.strip(): bucketlist = Bucketlist(title=title, intro=intro, owner=auth_data) bucketlist.save_bucketlist() response = bucketlist.serialize() return make_response(jsonify(response)), 201 else: response = {'message': 'You cant post empty bucketlist'} return make_response(jsonify(response)), 404
def post(self, id=None): """ Function to make a new bucketlist""" if id: abort(400, "This is a bad request, try again") self.reqparse.add_argument("name", type=str, required=True, help="Bucketlist name is required") args = self.reqparse.parse_args() name = args["name"] # Validating the user inputs if not is_not_empty(name): return {"message": "no blank fields allowed"}, 400 if name.isspace(): return { "message": "The name you have entered is not relevant" }, 400 # creating and saving an instance of a bucket bucket_instance = Bucketlist(name=name, user_id=g.user.id) save(bucket_instance) msg = (bucket_instance.name + "of ID" + str(bucket_instance.id) + " Has been \ saved successfully") return {"message": msg}, 201
def post(self): """create bucket lists""" self.reqparse = reqparse.RequestParser() self.reqparse.add_argument('title', type=str, required=True, help="Bucket list title cannot be empty") self.reqparse.add_argument('description', type=str, default="") args = self.reqparse.parse_args() title = args['title'] description = args['description'] if title == "": # if empty bad request status return {'error': "Bucket list title cannot be empty"}, 400 if Bucketlist.query.filter(Bucketlist.title.ilike(title)).first() and\ Bucketlist.query.filter_by(created_by=g.user.user_id).first() is not None: return {'message': 'the bucket list already exists'}, 400 bucketlist = Bucketlist(title=title, description=description, created_by=g.user.user_id) save(bucketlist) return {'message': 'bucketlist created successfuly'}, 201
def test_creating_bucketlist(self): """Tests successfully creating a bucketlist""" # this test would conflict with bucketlist defined in the setup # clear everythin before running it db.drop_all() db.create_all() # Instantiating a bucketlist object bucketlist = Bucketlist("Cook") # save the object to database db.session.add(bucketlist) db.session.commit() # asssert the attributes self.assertEqual(bucketlist.id, 1) self.assertEqual(bucketlist.name, "Cook") self.assertEqual(bucketlist.created_by, None) year = str(datetime.today().year) self.assertIn(year, str(bucketlist.date_created)) self.assertIn(year, str(bucketlist.date_modified)) self.assertEqual(len(bucketlist.items), 0) # test querying bucketlists bucketlist_query = Bucketlist.query.all() self.assertIn("<Bucketlist 'Cook'>", str(bucketlist_query)) self.assertFalse("<Bucketlist 'Random'>" in str(bucketlist_query))
def setUp(self): # setup the app and push app context: self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() # setup the db: db.drop_all() db.create_all() # create test datas: user = User( username='******', email='*****@*****.**', password='******' ) user.save() self.user = User(email="*****@*****.**", password="******", username="******") self.user.save() self.bucket_item = Bucketlist(name="Bucket List 1", user_id=self.user.id) self.bucket_item.save() self.user2 = User(email="*****@*****.**", password="******", username="******") self.user2.save() self.bucket_item2 = Bucketlist(name="Bucket List 2", user_id=self.user2.id) self.bucket_item2.save() bucket_item = Bucketlist(name="Bucket List", user_id=user.id) bucket_item.save() self.client = self.app.test_client() #default login details login_details = { 'email': '*****@*****.**', 'password': '******'} response = self.client.post( url_for('api.login'), headers=self.get_api_headers(), data=json.dumps(login_details) ) self.token = json.loads(response.data)['token']
def bucketlists(): # get the access token auth_header = request.headers.get('Authorization') access_token = auth_header.split(" ")[1] if access_token: user_id = User.decode_token(access_token) if not isinstance(user_id, str): # Go ahead and handle the request, the user is authed if request.method == "POST": name = str(request.data.get('name', '')) if name: bucketlist = Bucketlist(name=name, created_by=user_id) bucketlist.save() response = jsonify({ 'id': bucketlist.id, 'name': bucketlist.name, 'date_created': bucketlist.date_created, 'date_modified': bucketlist.date_modified, 'created_by': user_id }) return make_response(response), 201 else: # GET # get all the bucketlists for this user bucketlists = Bucketlist.get_all(user_id) results = [] for bucketlist in bucketlists: obj = { 'id': bucketlist.id, 'name': bucketlist.name, 'date_created': bucketlist.date_created, 'date_modified': bucketlist.date_modified, 'created_by': bucketlist.created_by } results.append(obj) return make_response(jsonify(results)), 200 else: # user is not legit, so the payload is an error message message = user_id response = {'message': message} return make_response(jsonify(response)), 401
def get(self): bucketlists = jsonify(Bucketlist.all()) if request.args.get('item') is not None: bucketlist_id = int(request.args.get('item')) table = Bucketlist.db.table('bucketlists') result = table.get(where('id') == bucketlist_id) if result is None: return jsonify({}) return jsonify(result) return bucketlists
def create_bucketlist(user): """create bucketlist --- tags: - "bucketlists" parameters: - in: "header" name: "Authorization" description: "Token of logged in user" required: true type: string - in: "body" name: "body" description: "Name and description of bucketlist" schema: type: "object" required: - name - description properties: name: type: "string" description: type: "string" responses: 400: description: "Failed" 201: description: "Success" """ name = request.data['name'] description = request.data['description'] owner = user['user_id'] try: new_bucketlist = Bucketlist(name, description, owner) new_bucketlist.save() response = new_bucketlist.to_json() return make_response(jsonify(response)), 201 except Exception as e: response = {'status': 'Failed', 'message': str(e)} return make_response(jsonify(response)), 400
def validate_create_bucket_list(self, data): """To alidate details required for bucket list creation.""" values = ["name"] for value in values: if data.get(value).isspace() or not data.get(value): return {'error': 'Invalid parameter.'}, 400 if Bucketlist.query.filter_by(name=data["name"].lower(), created_by=g.user.id).first(): return {'error': 'The bucketlist already exists'}, 409 bucketlist = Bucketlist(name=data["name"].lower(), created_by=g.user.id) db.session.add(bucketlist) db.session.commit() return { "msg": "Bucketlist created successfully.", "bucket": bucketlist.to_json() }, 201
def create_bucketlist(current_user): name = request.json.get('name') if not name: res = {"msg": "Please provide the bucketlistname"} return jsonify(res) else: bucketlist = Bucketlist(name=name, owner_id=current_user.id) db.session.add(bucketlist) db.session.commit() res = {"msg": "bucketlist added successfully"} return jsonify(res)
def bucket(): access_token = request.headers.get('Authorization') if access_token: user_id = User.decode_token(access_token) if isinstance(user_id, int): if request.method == "POST": data = request.get_json(force=True) name = data['name'] if name: bucketlist = Bucketlist(name=name, created_by=user_id) bucketlist.save() response = jsonify({ 'id': bucketlist.id, 'name': bucketlist.name, 'date_created': bucketlist.date_created, 'date_modified': bucketlist.date_modified, 'created_by': user_id }) return make_response(response), 201 else: bucketlists = Bucketlist.query.filter_by(created_by=user_id) results = [] for bucketlist in bucketlists: obj = { 'id': bucketlist.id, 'name': bucketlist.name, 'date_created': bucketlist.date_created, 'date_modified': bucketlist.date_modified, 'created_by': bucketlist.created_by } results.append(obj) return make_response(jsonify(results)), 200 else: message = user_id response = {'message': message} return make_response(jsonify(response)), 401
def get_bucketlists(): bucketlists = Bucketlist.get_all() results = [] for bucketlist in bucketlists: obj = { 'id': bucketlist.id, 'name': bucketlist.name, 'date_created': bucketlist.date_created, 'date_modified': bucketlist.date_modified } results.append(obj) response = jsonify(results) response.status_code = 200 return response
def bucketlists(): #check the type of request it receives if request.method == "POST": #CREATE BUCKETLIST OBJECT BY EXTRACTING NAME FROM THE REQUEST name=str(request.data.get('name','')) if name: bucketlist=Bucketlist(name=name) #save the created bucketlist bucketlist.save() response= jsonify({ 'id':bucketlist.id, 'name':bucketlist.name, 'date_created':bucketlist.date_created, 'date_modified':bucketlist.date_modified }) response.status_code= 201 return response #if request method is get else: bucketlists=Bucketlist.get_all()#this returns a list results=[] #iterate over the list of bucketlists for bucketlist in bucketlists: obj={ 'id':bucketlist.id, 'name':bucketlist.name, 'date_created':bucketlist.date_created, 'date_modified':bucketlist.date_modified } results.append(obj) response=jsonify(results) response.status_code=200 return response
def post(self): """POST request handling for /bucketlists/ Create a new bucketlist for the user """ name = str(request.data.get('name', '')) date = str(request.data.get('date', '')) description = str(request.data.get('description', '')) if name: bucketlist = Bucketlist(user_id, name, date, description) bucketlist.save() response = jsonify({ 'user': bucketlist.user_id, 'id': bucketlist.id, 'name': bucketlist.bucketlist_name, 'date': bucketlist.deadline_date, 'description': bucketlist.bucketlist_description }) response.headers['Access-Control-Allow-Origin'] = "*" response.headers['Access-Control-Allow-Credentials'] = True response.headers[ 'Access-Control-Allow-Headers'] = 'Content-Type, Authorization' response.headers['Access-Control-Allow-Methods'] = 'POST' response.status_code = 201 return response
def test_creating_bucketlist_with_a_missing_name(self): """Tests successfully creating a bucketlist""" # this test would conflict with bucketlist defined in the setup # clear everythin before running it db.drop_all() db.create_all() with self.assertRaises(Exception) as context: # Instantiating a bucketlist object bucketlist = Bucketlist() # save the object to database db.session.add(bucketlist) db.session.commit() self.assertEqual('kjfdjkgf', 'nndnnv')
def get_all_bucketlists(): bucketlists = Bucketlist.get_all() if not bucketlists: abort(404) results = [] for bucketlist in bucketlists: obj = { "id": bucketlist.id, "name": bucketlist.name, "date_created": bucketlist.date_created, "date_modified": bucketlist.date_modified } results.append(obj) response = jsonify(results) response.status_code = 200 return response
def post(self): """ Creates a bucketlist and saves it to the database """ args = self.reqparse.parse_args() new_bucketlist = Bucketlist(name=args['name'], created_by=g.user.id) if Bucketlist.query.filter_by(name=args['name'], created_by=g.user.id).first(): return errors.Conflict('Bucket list {} already exists'.format( args['name'])) if new_bucketlist: db.session.add(new_bucketlist) db.session.commit() return ({ 'message': 'New bucketlist created successfully', 'bucketlist': marshal(new_bucketlist, bucketlist_field) }, 201)
def setUp(self): super(ItemResourceTest, self).setUp() self.user = { "username": self.saved_user.username, "password": "******" } self.other_user = { "username": self.saved_user_2.username, "password": "******" } # create a bucketlist for tyrion current_user = User.query.filter_by( username=self.saved_user.username).first() self.example_bucketlist_one = Bucketlist( "bucketlist with items", "this is bucketlist has items", current_user.id) db.session.add(self.example_bucketlist_one) db.session.commit() self.bucketlist_with_items_id = Bucketlist.query.filter_by( name="bucketlist with items").first().id self.item_1 = Item('Item one', 'this is item one', self.bucketlist_with_items_id) self.item_2 = Item('Item two', 'this is item two', self.bucketlist_with_items_id) db.session.add(self.item_1) db.session.add(self.item_2) db.session.commit() self.item_one_id = Item.query.filter_by( title='Item one').first().id self.response = self.client.post( '/api/v1/auth/login', data=json.dumps(self.user), headers=self.headers) self.response_content = json.loads(self.response.data) self.headers['Authorization'] = 'JWT {}'.format( self.response_content['access_token'])
class BucketlistTestCase(unittest.TestCase): """Test for bucket list route""" def setUp(self): # setup the app and push app context: self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() # setup the db: db.drop_all() db.create_all() # create test datas: user = User( username='******', email='*****@*****.**', password='******' ) user.save() self.user = User(email="*****@*****.**", password="******", username="******") self.user.save() self.bucket_item = Bucketlist(name="Bucket List 1", user_id=self.user.id) self.bucket_item.save() self.user2 = User(email="*****@*****.**", password="******", username="******") self.user2.save() self.bucket_item2 = Bucketlist(name="Bucket List 2", user_id=self.user2.id) self.bucket_item2.save() bucket_item = Bucketlist(name="Bucket List", user_id=user.id) bucket_item.save() self.client = self.app.test_client() #default login details login_details = { 'email': '*****@*****.**', 'password': '******'} response = self.client.post( url_for('api.login'), headers=self.get_api_headers(), data=json.dumps(login_details) ) self.token = json.loads(response.data)['token'] def tearDown(self): db.session.remove() db.drop_all() self.app_context.pop() def get_api_headers(self, token=''): """Set default authorization headers for token""" return { 'Authorization': 'Basic ' + base64.b64encode( (token + ':' + '').encode('utf-8')).decode('utf-8'), 'Accept': 'application/json', 'Content-Type': 'application/json' } def test_for_bad_request(self): """Test for bad request """ new_bucket_item = { "bucketlist_name": "New Bucketlist" } response = self.client.post(url_for('api.bucketlists'),headers=self.get_api_headers(self.token), data=new_bucket_item ) self.assertEqual(response.status_code,400) def test_for_new_bucketlist_item(self): """Test for addition of new bucketlist collection""" new_bucket_item = { "bucketlist_name": "New Bucketlist" } response = self.client.post(url_for('api.bucketlists'),headers=self.get_api_headers(self.token), data=json.dumps(new_bucket_item) ) self.assertEqual(response.status_code,201) def test_for_method_not_allowed(self): """Test for method not allowed exception""" response = self.client.put(url_for('api.bucketlists'),headers=self.get_api_headers()) self.assertEqual(response.status_code,405) def test_for_protected_url(self): """Test guest cant access protected url""" response = self.client.get(url_for('api.bucketlists'),headers=self.get_api_headers()) self.assertEqual(response.status_code,401) def test_for_internal_server_error(self): """Test guest cant access protected url""" response = self.client.get(url_for('api.bucketlists'),headers=self.get_api_headers()) self.assertEqual(response.status_code,401) def test_for_not_authorized(self): """Test for bucketlist can only be accessed by the owner""" #for /api/v1/:id/ response = self.client.get(url_for('api.manage_bucketlist', id=1),headers=self.get_api_headers(self.token)) #for /api/v1/:id/items response2 = self.client.get(url_for('api.bucketlist_items', id=1),headers=self.get_api_headers(self.token)) self.assertEqual(response.status_code,401) self.assertEqual(response2.status_code,401) def test_get_bucketlist_authorized(self): """Test for authorized users can access bucket list""" response = self.client.get(url_for('api.manage_bucketlist', id=3),headers=self.get_api_headers(self.token)) self.assertEqual(response.status_code,200) def test_get_bucketlist_item_authorized(self): """Test for authorized users can access bucket list""" response = self.client.get(url_for('api.bucketlist_items',id=3),headers=self.get_api_headers(self.token)) self.assertEqual(response.status_code,200) def test_get_bucketlist_add_new_item_authorized(self): """Test for can add new item in the bucketlist """ new_item = { "item name": "New Item Bucketlist" } response = self.client.post(url_for('api.bucketlist_items', id=3),headers=self.get_api_headers(self.token), data=json.dumps(new_item)) self.assertEqual(response.status_code,201) def test_get_bucketlist_no_bucket_list(self): """Test for bucketlist collection doesn't exist""" response = self.client.get(url_for('api.bucketlist_items', id=5),headers=self.get_api_headers(self.token)) self.assertEqual(response.status_code,404) def test_get_edit_bucketlist(self): """ Test for edit bucketlist collection""" edit_bucket_item = { "bucketlist_name": "Edited Bucketlist" } response = self.client.put(url_for('api.manage_bucketlist', id=3),headers=self.get_api_headers(self.token), data=json.dumps(edit_bucket_item) ) response_data = json.loads(response.data) self.assertEqual(response.status_code,201) def test_delete_bucketlist(self): """ Test for delete bucket list""" response = self.client.delete(url_for('api.manage_bucketlist', id=3),headers=self.get_api_headers(self.token) ) self.assertEqual(response.status_code,204) def test_for_bucket_list_item_not_found(self): """ test for get bucketlist item route /api/v1/:id/items not found """ response = self.client.get(url_for('api.manage_bucketlist', id=5),headers=self.get_api_headers(self.token)) self.assertEqual(response.status_code,404) def test_for_url_not_found(self): """ test for none existing url within the api applicaito """ response = self.client.get(url_for('api.bucketlists') + '/custompage', headers=self.get_api_headers(self.token)) self.assertEqual(response.status_code,404)
class ModelTestCase(unittest.TestCase): """Test cases for the models """ def setUp(self): self.app = create_app('testing') self.app_context = self.app.app_context() self.app_context.push() db.create_all() self.user = User(email="*****@*****.**", password="******") self.user.save() self.bucket_item = Bucketlist(name="Bucket List 1", user_id=self.user.id) self.bucket_item.save() self.item = Item(name="Needs of bucket items", bucketlist_id=self.bucket_item.id) self.item.save() def tearDown(self): db.session.remove() db.drop_all() self.app_context.pop() def test_for_data_exsitence(self): """Test to check if the data were created""" bucketlist = Bucketlist.query.all() self.assertEqual(len(bucketlist),1) def test_for_data_delete(self): """ Test for the delete feature""" self.bucket_item.delete() bucketlist = Bucketlist.query.all() self.assertNotEqual(len(bucketlist),1) def test_for_edit_feature(self): """ test for edit feature """ self.item.edit("My Test Item") self.item.save() self.bucket_item.edit("My Test Item") self.bucket_item.save() self.assertEqual(self.bucket_item.name, "My Test Item") self.assertEqual(self.item.name, "My Test Item") def test_password_setter(self): """Test for password setter""" u = User(password='******') self.assertTrue(u.password_hash is not None) def test_no_password_getter(self): """ Test for password raises error""" u = User(password='******') with self.assertRaises(AttributeError): u.password def test_password_salts_are_random(self): """Test password salts are random""" u = User(password='******') u2 = User(password='******') self.assertTrue(u.password_hash != u2.password_hash) def test_user_str(self): """ Test string function of user """ user = str(self.user) self.assertIsNotNone(user) def test_for_to_json(self): """ Test to user_json """ #user to json user_json = self.user.to_json() #bucket-json to json bucket_json = self.bucket_item.to_json() #itme json to json item_json = self.item.to_json() self.assertIsNotNone(item_json) self.assertIsNotNone(bucket_json) self.assertIsNotNone(user_json)