def find_or_create_user(uuid: str, username: str, session: Session, commit: bool = True) -> User: """ Returns a user object representing the referenced journalist UUID. If the user does not already exist in the data, a new instance is created. If the user exists but user fields have changed, the db is updated. """ user = session.query(User).filter_by(uuid=uuid).one_or_none() if not user: new_user = User(username=username) new_user.uuid = uuid session.add(new_user) if commit: session.commit() return new_user if user.username != username: user.username = username if commit: session.commit() return user
def test_source_collection_ordering_with_multiple_draft_replies(): # Create some test submissions, replies, and draft replies. source = factory.Source() file_1 = File(source=source, uuid="test", size=123, filename="1-test.doc.gpg", download_url='http://test/test') message_2 = Message(source=source, uuid="test", size=123, filename="2-test.doc.gpg", download_url='http://test/test') user = User(username='******') reply_3 = Reply(source=source, journalist=user, filename="3-reply.gpg", size=1234, uuid='test') draft_reply_4 = DraftReply(uuid='4', source=source, journalist=user, file_counter=3, timestamp=datetime.datetime(2000, 6, 6, 6, 0)) draft_reply_5 = DraftReply(uuid='5', source=source, journalist=user, file_counter=3, timestamp=datetime.datetime(2001, 6, 6, 6, 0)) reply_6 = Reply(source=source, journalist=user, filename="4-reply.gpg", size=1234, uuid='test2') draft_reply_7 = DraftReply(uuid='6', source=source, journalist=user, file_counter=4, timestamp=datetime.datetime(2002, 6, 6, 6, 0)) source.files = [file_1] source.messages = [message_2] source.replies = [reply_3, reply_6] source.draftreplies = [draft_reply_4, draft_reply_5, draft_reply_7] # Now these items should be in the source collection in the proper order assert source.collection[0] == file_1 assert source.collection[1] == message_2 assert source.collection[2] == reply_3 assert source.collection[3] == draft_reply_4 assert source.collection[4] == draft_reply_5 assert source.collection[5] == reply_6 assert source.collection[6] == draft_reply_7
def test_source_server_collection(): # Create some test submissions and replies source = factory.Source() file_ = File(source=source, uuid="test", size=123, filename="2-test.doc.gpg", download_url='http://test/test') message = Message(source=source, uuid="test", size=123, filename="3-test.doc.gpg", download_url='http://test/test') user = User(username='******') reply = Reply(source=source, journalist=user, filename="1-reply.gpg", size=1234, uuid='test') draft_reply = DraftReply(source=source, journalist=user, uuid='test', timestamp=datetime.datetime(2002, 6, 6, 6, 0)) source.files = [file_] source.messages = [message] source.replies = [reply] source.draftreplies = [draft_reply] # Now these items should be in the source collection in the proper order assert source.server_collection[0] == reply assert source.server_collection[1] == file_ assert source.server_collection[2] == message # Drafts do not appear in the server_collection, they are local only. assert draft_reply not in source.server_collection
def test_string_representation_of_draft_reply(): user = User(username='******') source = factory.Source() draft_reply = DraftReply(source=source, journalist=user, uuid='test') draft_reply.__str__() draft_reply.content = "hello" draft_reply.__str__()
def test_source_collection(): # Create some test submissions and replies source = factory.Source() file_ = File( source=source, uuid="test", size=123, filename="2-test.doc.gpg", download_url="http://test/test", ) message = Message( source=source, uuid="test", size=123, filename="3-test.doc.gpg", download_url="http://test/test", ) user = User(username="******") reply = Reply(source=source, journalist=user, filename="1-reply.gpg", size=1234, uuid="test") source.files = [file_] source.messages = [message] source.replies = [reply] # Now these items should be in the source collection in the proper order assert source.collection[0] == reply assert source.collection[1] == file_ assert source.collection[2] == message
def test_string_representation_of_reply(): user = User(username='******') source = factory.Source() reply = Reply(source=source, journalist=user, filename="1-reply.gpg", size=1234, uuid='test') reply.__str__() reply.content = "hello" reply.__str__()
def test_repr_representation_of_reply(): user = User(username="******") source = factory.Source() reply = Reply(source=source, journalist=user, filename="1-reply.gpg", size=1234, uuid="test") reply.__repr__()
def test_user_fullname(): user1 = User(username='******', firstname='firstname_mock', lastname='lastname_mock') user2 = User(username='******', firstname='firstname_mock') user3 = User(username='******', lastname='lastname_mock') user4 = User(username='******') assert user1.fullname == 'firstname_mock lastname_mock' assert user2.fullname == 'firstname_mock' assert user3.fullname == 'lastname_mock' assert user4.fullname == 'username_mock' user1.__repr__()
def find_or_create_user(uuid, username, session): """ Returns a user object representing the referenced journalist UUID. If the user does not already exist in the data, a new instance is created. If the user exists but the username has changed, the username is updated. """ user = session.query(User).filter_by(uuid=uuid).one_or_none() if user and user.username == username: # User exists in the local database and the username is unchanged. return user elif user and user.username != username: # User exists in the local database but the username is changed. user.username = username session.add(user) session.commit() return user else: # User does not exist in the local database. new_user = User(username) new_user.uuid = uuid session.add(new_user) session.commit() return new_user
def test_user_fullname(): user1 = User(username="******", firstname="firstname_mock", lastname="lastname_mock") user2 = User(username="******", firstname="firstname_mock") user3 = User(username="******", lastname="lastname_mock") user4 = User(username="******") assert user1.fullname == "firstname_mock lastname_mock" assert user2.fullname == "firstname_mock" assert user3.fullname == "lastname_mock" assert user4.fullname == "username_mock" user1.__repr__()
def test_source_collection(): # Create some test submissions and replies source = factory.Source() submission = Submission(source=source, uuid="test", size=123, filename="2-test.doc.gpg", download_url='http://test/test') user = User('hehe') reply = Reply(source=source, journalist=user, filename="1-reply.gpg", size=1234, uuid='test') source.submissions = [submission] source.replies = [reply] # Now these items should be in the source collection in the proper order assert source.collection[0] == reply assert source.collection[1] == submission
def test_user_initials(): # initials should be first char of firstname followed by first char of last name user1 = User(username='******', firstname='firstname_mock', lastname='lastname_mock') user2 = User(username='******', firstname='firstname_mock', lastname='l') user3 = User(username='******', firstname='f', lastname='lastname_mock') user4 = User(username='******', firstname='f', lastname='l') assert user1.initials == 'fl' assert user2.initials == 'fl' assert user3.initials == 'fl' assert user4.initials == 'fl' # initials should be first two chars of username user5 = User(username='******') user6 = User(username='******', firstname='f') user7 = User(username='******', lastname='l') assert user5.initials == 'us' assert user6.initials == 'us' assert user7.initials == 'us' # initials should be first two chars of firstname or lastname user8 = User(username='******', firstname='firstname_mock') user9 = User(username='******', lastname='lastname_mock') assert user8.initials == 'fi' assert user9.initials == 'la' user1.__repr__()
def test_string_representation_of_user(): user = User('hehe') user.__repr__()
def test_user_initials(): # initials should be first char of firstname followed by first char of last name user1 = User(username="******", firstname="firstname_mock", lastname="lastname_mock") user2 = User(username="******", firstname="firstname_mock", lastname="l") user3 = User(username="******", firstname="f", lastname="lastname_mock") user4 = User(username="******", firstname="f", lastname="l") assert user1.initials == "fl" assert user2.initials == "fl" assert user3.initials == "fl" assert user4.initials == "fl" # initials should be first two chars of username user5 = User(username="******") user6 = User(username="******", firstname="f") user7 = User(username="******", lastname="l") assert user5.initials == "us" assert user6.initials == "us" assert user7.initials == "us" # initials should be first two chars of firstname or lastname user8 = User(username="******", firstname="firstname_mock") user9 = User(username="******", lastname="lastname_mock") assert user8.initials == "fi" assert user9.initials == "la" user1.__repr__()
def test_repr_representation_of_draft_reply(): user = User(username="******") source = factory.Source() draft_reply = DraftReply(source=source, journalist=user, uuid="test") draft_reply.__repr__()