def adherentExists(session, username): """ Returns true if the user exists """ try: Adherent.find(session, username) except UserNotFound: return False return True
def getUser(admin, username): """ [API] Get the specified user from the database """ s = db.get_db().get_session() try: return dict(Adherent.find(s, username)) except UserNotFound: return NoContent, 404
def member2(): yield Adherent( nom='Reignier', prenom='Edouard', mail='*****@*****.**', login='******', password='******', )
def member1(): yield Adherent( nom='Dubois', prenom='Jean-Louis', mail='*****@*****.**', login='******', password='******', )
def sample_member(sample_room): yield Adherent( nom='Dubois', prenom='Jean-Louis', mail='*****@*****.**', login='******', password='******', chambre=sample_room, )
def sample_member2(sample_room2): yield Adherent( nom='Reignier', prenom='Edouard', mail='*****@*****.**', login='******', commentaires='Desauthent pour routeur', password='******', chambre=sample_room2, )
def sample_member3(sample_room2): """ Membre sans chambre """ yield Adherent( nom='Robert', prenom='Dupond', mail='*****@*****.**', login='******', commentaires='a', password='******', )
def update_wireless_device(admin, macAddress, body, s): """ Update a wireless device in the database """ q = s.query(Portable).filter(Portable.mac == macAddress) dev = q.one() dev.start_modif_tracking() dev.mac = body['mac'] dev.adherent = Adherent.find(s, body['username']) s.flush() Modification.add_and_commit(s, dev.adherent, dev.get_ruby_modif(), admin)
def create_wireless_device(admin, body, s): """ Create a wireless device in the database """ dev = Portable( mac=body['mac'], adherent=Adherent.find(s, body['username']), ) s.add(dev) s.flush() Modification.add_and_commit(s, dev.adherent, dev.get_ruby_modif(), admin)
def create_wired_device(admin, body, s): """ Create a wired device in the database """ dev = Ordinateur( mac=body['mac'], ip=body['ipAddress'], ipv6=body['ipv6Address'], adherent=Adherent.find(s, body['username']), ) s.add(dev) s.flush() Modification.add_and_commit(s, dev.adherent, dev.get_ruby_modif(), admin)
def putUser(admin, username, body): """ [API] Create/Update user from the database """ s = db.get_db().get_session() # Create a valid object try: new_user = Adherent.from_dict(s, body) except InvalidEmail: return "Invalid email", 400 except RoomNotFound: return "No room found", 400 except ValueError: return "String must not be empty", 400 try: # Check if it already exists update = adherentExists(s, username) if update: current_adh = Adherent.find(s, username) new_user.id = current_adh.id current_adh.start_modif_tracking() # Merge the object (will create a new if it doesn't exist) new_user = s.merge(new_user) s.flush() # Create the corresponding modification Modification.add_and_commit(s, new_user, new_user.get_ruby_modif(), admin) except Exception: s.rollback() raise if update: return NoContent, 204 else: return NoContent, 201
def test_modification_delete_member(api_client, sample_member): s = db.get_db().get_session() a = Adherent.find(s, sample_member.login) a.start_modif_tracking() s.delete(a) s.flush() # Build the corresponding modification Modification.add_and_commit(s, a, a.get_ruby_modif(), Utilisateur.find_or_create(s, "test")) q = s.query(Modification) m = q.first() assert m.action == ( '--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\n' 'chambre_id:\n' '- 1\n' '- \n' 'id:\n' '- 1\n' '- \n' 'login:\n' '- dubois_j\n' '- \n' 'mail:\n' '- [email protected]\n' '- \n' 'mode_association:\n' '- 2011-04-30 17:50:17\n' '- \n' 'nom:\n' '- Dubois\n' '- \n' 'password:\n' '- a\n' '- \n' 'prenom:\n' '- Jean-Louis\n' '- \n') assert m.adherent_id == sample_member.id now = datetime.datetime.now() one_sec = datetime.timedelta(seconds=1) assert now - m.created_at < one_sec assert now - m.updated_at < one_sec assert m.utilisateur_id == 1
def addMembership(admin, username, body): """ [API] Add a membership record in the database """ s = db.get_db().get_session() start = string_to_date(body["start"]) end = None if start and "duration" in body: duration = body["duration"] end = start + datetime.timedelta(days=duration) try: s.add( Adhesion(adherent=Adherent.find(s, username), depart=start, fin=end)) except UserNotFound: return NoContent, 404 s.commit() return NoContent, 200, {'Location': 'test'} # TODO: finish that!
def updatePassword(admin, username, body): password = body["password"] s = db.get_db().get_session() try: a = Adherent.find(s, username) except UserNotFound: return NoContent, 404 try: a.start_modif_tracking() a.password = ntlm_hash(password) s.flush() # Build the corresponding modification Modification.add_and_commit(s, a, a.get_ruby_modif(), admin) except Exception: s.rollback() raise return NoContent, 204
def test_modification_multiple_changes_updated(api_client, sample_member): s = db.get_db().get_session() a = Adherent.find(s, sample_member.login) a.start_modif_tracking() a.commentaires = "Hey I am a comment" a.nom = "Test" a.prenom = "Test" a.mail = "*****@*****.**" s.flush() # Build the corresponding modification Modification.add_and_commit(s, a, a.get_ruby_modif(), Utilisateur.find_or_create(s, "test")) q = s.query(Modification) m = q.first() assert m.action == ( '--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\n' 'commentaires:\n' '- \n' '- Hey I am a comment\n' 'mail:\n' '- [email protected]\n' '- [email protected]\n' 'nom:\n' '- Dubois\n' '- Test\n' 'prenom:\n' '- Jean-Louis\n' '- Test\n') assert m.adherent_id == sample_member.id now = datetime.datetime.now() one_sec = datetime.timedelta(seconds=1) assert now - m.created_at < one_sec assert now - m.updated_at < one_sec assert m.utilisateur_id == 1
def deleteUser(admin, username): """ [API] Delete the specified User from the database """ s = db.get_db().get_session() # Find the soon-to-be deleted user try: a = Adherent.find(s, username) except UserNotFound: return NoContent, 404 try: # if so, start tracking for modifications a.start_modif_tracking() # Actually delete it s.delete(a) s.flush() # Write it in the modification table Modification.add_and_commit(s, a, a.get_ruby_modif(), admin) except Exception: s.rollback() raise return NoContent, 204
def test_modification_pass_updated(api_client, sample_member): s = db.get_db().get_session() a = Adherent.find(s, sample_member.login) a.start_modif_tracking() a.password = "******" s.flush() # Build the corresponding modification Modification.add_and_commit(s, a, a.get_ruby_modif(), Utilisateur.find_or_create(s, "test")) q = s.query(Modification) m = q.first() assert m.action == ( '--- !ruby/hash:ActiveSupport::HashWithIndifferentAccess\n' 'password:\n' '- a\n' '- TESTESTEST\n') assert m.adherent_id == sample_member.id now = datetime.datetime.now() one_sec = datetime.timedelta(seconds=1) assert now - m.created_at < one_sec assert now - m.updated_at < one_sec assert m.utilisateur_id == 1