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 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))