def put(self, note_id): request_data = request.get_json() note = session.query(Note).get(note_id) if note is None: return Message.instance_not_exist() is_moderator = False for moderator in note.users: if moderator.id == auth.current_user().id: is_moderator = True if note.owner_id != auth.current_user().id and not is_moderator: return Message.auth_failed() if 'name' in request_data: note.name = request_data['name'] if 'text' in request_data: note.text = request_data['text'] if 'tag_id' in request_data: tag_id = request_data['tag_id'] tag = session.query(Tag).get(tag_id) note.tags.append(tag) session.commit() return Message.successful('updated')
def post(self): request_data = request.get_json() try: name = request_data['name'] text = request_data['text'] if not isinstance(auth.current_user(), User): raise Exception owner_id = auth.current_user().id except KeyError: return Message.value_error() except Exception: return Message.creation_error() if note_schema.validate(data=request_data, session=session): return Message.creation_error() note = Note( name=name, text=text, owner_id=owner_id) if note is None: return Message.creation_error() session.add(note) session.commit() return Message.successful('created', 201)
def delete(self, tag_id): tag = session.query(Tag).get(tag_id) if tag is None: return Message.instance_not_exist() session.delete(tag) session.commit() return Message.successful('deleted')
def put(self, note_id, user_id): note = session.query(Note).get(note_id) if note is None: return Message.instance_not_exist() user = session.query(User).get(user_id) if user is None: return Message.instance_not_exist() is_moderator = False for moderator in note.users: if moderator.id == auth.current_user().id: is_moderator = True if note.owner_id != auth.current_user().id and not is_moderator: return Message.auth_failed() max_moderators = 5 if note.number_of_moderators >= max_moderators: return Message.message('Can not add a moderator', 400) note.number_of_moderators += 1 note.users.append(user) session.commit() return Message.successful('add moderator to note')
def delete(self, note_id): note = session.query(Note).get(note_id) if note is None: return Message.instance_not_exist() if note.owner_id != auth.current_user().id: return Message.auth_failed() session.delete(note) session.commit() return Message.successful('deleted')
def put(self, tag_id): request_data = request.get_json() tag = session.query(Tag).get(tag_id) if tag is None: return Message.instance_not_exist() if 'name' in request_data: tag.name = request_data['name'] session.commit() return Message.successful('updated')
def delete(self, user_id): user = session.query(User).get(user_id) if user is None: return Message.instance_not_exist() if user.email != auth.current_user().email: return Message.auth_failed() session.delete(user) session.commit() return Message.successful('deleted')
def get(self): tag_list = session.query(Tag).all() if not tag_list: return Message.instance_not_exist() return tag_list_schema.dump(tag_list), 200
def test_note_user_over(self): users = [] for i in range(1, 6): users.append( User(username=f"user{i+1}", email=f"user{i+1}@test.com", password=f"user{i+1}test")) note = create_note(self) note.users = users note.number_of_moderators = 5 user6 = User(username=f"user{10}", email=f"user{10}@test.com", password=f"user{10}test") self.session.add(user6) self.session.commit() response = self.client.put( f"/api/add/note/{note.id}/user/{user6.id}/", content_type='application/json', headers={ 'Authorization': 'Basic ' + base64.b64encode(b'[email protected]:user1test').decode('utf-8') }) self.assertEqual((response.get_json(), 400), Message.message('Can not add a moderator', 400))
def get(self): note_list = session.query(Note).all() if not note_list: return Message.instance_not_exist() return note_list_schema.dump(note_list), 200
def get(self, user_id): user = session.query(User).get(user_id) if user is None: return Message.instance_not_exist() user_note_list = user.notes return note_list_schema.dump(user_note_list)
def get(self, tag_id): tag = session.query(Tag).get(tag_id) if tag is None: return Message.instance_not_exist() return tag_schema.dump(tag), 200
def get(self): user_list = session.query(User).all() if not user_list: return Message.instance_not_exist() return user_list_schema.dump(user_list), 200
def test_userNotesList_404(self): user = create_user(self) response = self.client.get(f"/api/user/{user.id+2}/notes/") self.assertEqual((response.get_json(), 404), Message.instance_not_exist())
def test_delete_tag(self): tag = create_tag(self) response = self.client.delete(f"/api/tag/{tag.id}/") self.assertEqual((response.get_json(), 200), Message.successful('deleted'))
def test_get_tag_404(self): tag = create_tag(self) response = self.client.get(f"/api/tag/{tag.id + 2}/") self.assertEqual((response.get_json(), 404), Message.instance_not_exist())
def test_get_note_404(self): note = create_note(self) response = self.client.get(f"/api/note/{note.id + 2}/") self.assertEqual((response.get_json(), 404), Message.instance_not_exist())
def test_create_user_value_error(self): response = self.client.post("/api/user/create/", data=json.dumps( dict(username="******", password="******")), content_type='application/json') self.assertEqual((response.get_json(), 400), Message.value_error())
def put(self, note_id, tag_id): note = session.query(Note).get(note_id) if note is None: return Message.instance_not_exist() if note.owner_id != auth.current_user().id: return Message.auth_failed() tag = session.query(Tag).get(tag_id) if tag is None: return Message.instance_not_exist() note.tags.append(tag) session.commit() return Message.successful('add tag to note')
def put(self, user_id): request_data = request.get_json() user = session.query(User).get(user_id) if user is None: return Message.instance_not_exist() if user.email != auth.current_user().email: return Message.auth_failed() if 'username' in request_data: user.username = request_data['username'] if 'email' in request_data: user.email = request_data['email'] if 'password' in request_data: user.password = request_data["password"] session.commit() return Message.successful('updated')
def test_create_user_succesful(self): response = self.client.post("/api/user/create/", data=json.dumps( dict(username="******", email="*****@*****.**", password="******")), content_type='application/json') self.assertEqual((response.get_json(), 201), Message.successful('created', 201))
def test_create_tag_value_error(self): response = self.client.post( "/api/tag/", data=json.dumps(dict(name=None, )), content_type='application/json', headers={ 'Authorization': 'Basic ' + base64.b64encode(b'[email protected]:user1test').decode('utf-8') }) self.assertEqual((response.get_json(), 400), Message.value_error())
def post(self): request_data = request.get_json() try: username = request_data['username'] email = request_data['email'] password = request_data['password'] except KeyError: return Message.value_error() user = User(username=username, email=email, password=password) if user is None: return Message.creation_error() session.add(user) try: session.commit() except IntegrityError: session.rollback() return Message.creation_error() return Message.successful('created', 201)
def test_delete_note(self): note = create_note(self) response = self.client.delete( f"/api/note/{note.id}/", headers={ 'Authorization': 'Basic ' + base64.b64encode(b'[email protected]:user1test').decode('utf-8') }) self.assertEqual((response.get_json(), 200), Message.successful('deleted'))
def post(self): request_data = request.get_json() try: name = request_data['name'] except KeyError: return Message.value_error() if tag_schema.validate(data=request_data, session=session): return Message.value_error() tag = Tag(name=name) if tag is None: return Message.creation_error() session.add(tag) try: session.commit() except IntegrityError: session.rollback() return Message.creation_error() return Message.successful('created', 201)
def test_create_tag_succesful(self): response = self.client.post( "/api/tag/", data=json.dumps(dict(name="tag1", )), content_type='application/json', headers={ 'Authorization': 'Basic ' + base64.b64encode(b'[email protected]:user1test').decode('utf-8') }) self.assertEqual((response.get_json(), 201), Message.successful('created', 201))
def test_delete_tag_404(self): tag = create_tag(self) response = self.client.delete( f"/api/tag/{tag.id + 2}/", headers={ 'Authorization': 'Basic ' + base64.b64encode(b'[email protected]:user1test').decode('utf-8') }) self.assertEqual((response.get_json(), 404), Message.instance_not_exist())
def test_put_note(self): note = create_note(self) response = self.client.put( f"/api/note/{note.id}/", data=json.dumps(dict(name="note1_edited")), content_type='application/json', headers={ 'Authorization': 'Basic ' + base64.b64encode(b'[email protected]:user1test').decode('utf-8') }) self.assertEqual((response.get_json(), 200), Message.successful('updated'))
def test_put_tag_404(self): tag = create_tag(self) response = self.client.put( f"/api/tag/{tag.id + 2}/", data=json.dumps(dict(name="tag1_edited")), content_type='application/json', headers={ 'Authorization': 'Basic ' + base64.b64encode(b'[email protected]:user1test').decode('utf-8') }) self.assertEqual((response.get_json(), 404), Message.instance_not_exist())
def test_note_404_tag(self): tag = create_tag(self) note = create_note(self) response = self.client.put( f"/api/add/note/{note.id+2}/tag/{tag.id}/", content_type='application/json', headers={ 'Authorization': 'Basic ' + base64.b64encode(b'[email protected]:user1test').decode('utf-8') }) self.assertEqual((response.get_json(), 404), Message.instance_not_exist())