def addusercommand(): cmd_id = request.form.get('cmd_id') url = request.form.get('url') name = request.form.get('name') if cmd_id == "" or url == "" or name == "" or \ cmd_id is None or url is None or name is None: return 'A value is missing', 400 try: command = Command( cmd_id=cmd_id, name=name, url=url, owner=current_user.id, creator=current_user.id ) db.session.add(command) db.session.commit() return objectToJson(command), 200 except IntegrityError: db.session.rollback() return 'Command ID already exists', 400 except Exception as e: db.session.rollback() return 'Unexpected Error', 400
def test_updateusercommand(self): test_url = '/dashboard/_updateusercommand' self.basic_check( test_url, dict(cmd_id='p'), 'A value is missing' ) with self.client: self.client.post( '/login', data=dict(username="******", password="******"), follow_redirects=True ) # updating non-existent command response = self.client.post( test_url, data=dict(cmd_id='g',url="http://www.google.com/search?q=%s", name="Google Search") ) self.assertEqual(response.status_code, 400) self.assertEqual(response.data,'Unexpected Error') # normal behavior response = self.client.post( 'dashboard/_addusercommand', data=dict(cmd_id='g',url="http://www.google.com/search?q=%s", name="Google Search") ) self.assertEqual(response.status_code, 200) self.assertEqual(response.data,'{"name": "Google Search", ' '"creator": 2, "url": "http://www.google.com/search?q=%s",' ' "cmd_id": "g", "owner": 2, "id": 1}') response = self.client.post( test_url, data=dict(cmd_id='g',url="http://www.bing.com/search?q=%s", name="Trolling") ) self.assertEqual(response.status_code, 200) self.assertEqual(response.data,'{"name": "Trolling", "creator": 2, ' '"url": "http://www.bing.com/search?q=%s", "cmd_id": "g", ' '"owner": 2, "id": 1}') self.assertEqual(response.data, objectToJson(Command.query.filter_by().first()))
def updateusercommand(): cmd_id = request.form.get('cmd_id') url = request.form.get('url') name = request.form.get('name') if cmd_id == "" or url == "" or name == "" or \ cmd_id is None or url is None or name is None: return 'A value is missing', 400 conditions = {'owner':current_user.get_id(), 'cmd_id':cmd_id} updates = {} if url is not None: updates['url'] = url if name is not None: updates['name'] = name try: db.session.query(Command).filter_by(**conditions).update(updates) db.session.commit() return objectToJson(Command.query.filter_by(**conditions).first()), 200 except Exception as e: db.session.rollback() return 'Unexpected Error', 400