def first_message_to_user(content, email, device): try: user = db.session.query(UserModel).filter( UserModel.email == email).first() if user is None: raise e_account.UserNotFound elif not device.circle.has_member(user): raise e_cirle.UserNotPartOfCircle conversation = Conversation(device_access=True) conversation.circle = device.circle conversation.name = "NewConversation" link = UserToConversation(privilege="ADMIN") link.user = user link.conversation = conversation message = Message(content=content["text_message"] if "text_message" in content else "", is_user=False) message.conversation = conversation message.device = device media_list = [] if "files" in content: for file in content["files"]: media = Media() media.identifier = file MessageToMedia(message=message, media=media) db.session.commit() media_list.append(media.get_simple_content()) db.session.commit() sockets.notify_user(user, False, 'conversation', { "conversation_id": conversation.id, "event": 'invite' }) info_and_message = "[" + conversation.name + "] " + device.name + " : " + str( message.text_content) try: messenger_conversation_model_send(0, conversation, info_and_message) except Exception: pass try: hangout_conversation_model_send(0, conversation, info_and_message) except Exception: pass response = { "data": { "success": True, 'media_list': media_list, 'message_id': message.id }, "status_code": 200 } except (e_cirle.CircleException, e_account.AccountException) as exception: response = { "data": { "success": False, "message": exception.message }, "status_code": exception.status_code } return response
def setUp(self): neo_config.load_config() neo_config.set_project_variables() self.neo = NeoAPI(neo_config) self.api = self.neo.activate_testing() self.client = SocketIOTestClient(self.neo.app, socketio) self.client2 = SocketIOTestClient(self.neo.app, socketio) self.deviceClient = SocketIOTestClient(self.neo.app, socketio) self.client.disconnect() self.client2.disconnect() self.deviceClient.disconnect() self.user1 = db.session.query(UserModel).filter( UserModel.email == "*****@*****.**").first() if self.user1 is None: self.user1 = UserModel(email="*****@*****.**", password="******", first_name="firstname", last_name="lastname", birthday="1995-12-12") self.user2 = db.session.query(UserModel).filter( UserModel.email == "*****@*****.**").first() if self.user2 is None: self.user2 = UserModel(email="*****@*****.**", password="******", first_name="firstname", last_name="lastname", birthday="1995-12-12") self.email = self.user2.email self.circle = Circle(name="Mamie") self.linkCircle = UserToCircle(user=self.user1, circle=self.circle, privilege="ADMIN") self.linkCircle2 = UserToCircle(user=self.user2, circle=self.circle) self.conversation = Conversation("test") self.conversation.device_access = True self.conversation.circle = self.circle self.link = UserToConversation(user=self.user1, conversation=self.conversation, privilege='ADMIN') self.link2 = UserToConversation(user=self.user2, conversation=self.conversation) self.device = Device(name="Papie") self.device.circle = self.circle self.device_password = self.device.get_pre_activation_password() self.device.activate(self.device.key) db.session.commit() self.circle_id = self.circle.id self.conversation_id = self.conversation.id self.token1 = authenticate_user(self.api, self.user1, "test") self.token2 = authenticate_user(self.api, self.user2, "test") self.device_token = authenticate_device(self.api, self.device, self.device_password) self.tokenAdmin = authenticate_user(self.api, "*****@*****.**", "PapieNeo2019")
def invite(conversation_id, email, client, is_device): try: if not is_device: link = db.session.query(UserToConversation).filter( UserToConversation.conversation_id == conversation_id, UserToConversation.user_id == client.id).first() if link is None: raise e_conversation.UserForbiddenAccess conversation = link.conversation else: conversation = db.session.query(Conversation).filter( Conversation.id == conversation_id).first() if conversation is None: raise e_conversation.ConversationNotFound recipient = db.session.query(UserModel).filter( UserModel.email == email).first() if recipient is None: raise e_conversation.SpecifiedUserNotFound temp = db.session.query(UserToConversation).filter( UserToConversation.conversation_id == conversation_id, UserToConversation.user_id == recipient.id).first() if temp is not None: raise e_conversation.UserAlreadyPartOfConversation if not conversation.circle.has_member(recipient): raise e_circle.UserNotPartOfCircle new_link = UserToConversation(privilege="STANDARD") new_link.user = recipient new_link.conversation = conversation db.session.commit() recipient.notify_me(title="Conversation", body=("Vous êtes invité dans la conversation %s" % (conversation.name))) conversation.mobile_notification( title="Conversation", body=("%s à rejoins la conversation %s" % (recipient.first_name, conversation.name))) sockets.notify_user(client=recipient, is_device=False, p1='conversation', p2={ 'event': 'invite', 'conversation_id': conversation_id }) response = {"data": {"success": True}, "status_code": 200} except (e_conversation.ConversationException, e_circle.CircleException) as exception: response = { "data": { "success": False, "message": exception.message }, "status_code": exception.status_code } return response
def setUp(self): neo_config.load_config() neo_config.set_project_variables() neo = NeoAPI(neo_config) self.api = neo.activate_testing() self.user1 = db.session.query(UserModel).filter( UserModel.email == "*****@*****.**").first() if self.user1 is None: self.user1 = UserModel(email="*****@*****.**", password="******", first_name="firstname", last_name="lastname", birthday="1995-12-12") self.user2 = db.session.query(UserModel).filter( UserModel.email == "*****@*****.**").first() if self.user2 is None: self.user2 = UserModel(email="*****@*****.**", password="******", first_name="firstname", last_name="lastname", birthday="1111-11-11") self.user3 = db.session.query(UserModel).filter( UserModel.email == "*****@*****.**").first() if self.user3 is None: self.user3 = UserModel(email="*****@*****.**", password="******", first_name="firstname", last_name="lastname", birthday="1111-11-11") self.circle = Circle(name="TestConversationinvite") self.link = UserToCircle() self.link.user = self.user1 self.link.circle = self.circle self.link2 = UserToCircle() self.link2.user = self.user2 self.link2.circle = self.circle self.conv = Conversation() self.conv.circle = self.circle self.utc1 = UserToConversation(privilege="ADMIN") self.utc1.user = self.user1 self.utc1.conversation = self.conv self.utc2 = UserToConversation(privilege="STANDARD") self.utc2.user = self.user2 self.utc2.conversation = self.conv db.session.commit() self.token1 = authenticate_user(self.api, self.user1, "test") self.token2 = authenticate_user(self.api, self.user2, "test") self.token3 = authenticate_user(self.api, self.user3, "test")
def test_change_admin(self): utc = UserToConversation() utc.user = self.user3 utc.conversation = self.conv db.session.commit() json_data = { "token": self.token1, "conversation_id": self.conv.id, } response = self.api.post('/conversation/quit', data=json.dumps(json_data), content_type='application/json') response_json = json.loads(response.data) assert response.status_code == 200 assert response_json['success'] assert (utc.privilege == "ADMIN" or self.utc2.privilege == "ADMIN")
def test_already_existing_user(self): utc = UserToConversation() utc.user = self.user2 utc.conversation = self.conv db.session.commit() json_data = { "token": self.token1, "conversation_id": self.conv.id, "email": self.user2.email } response = self.api.post('/conversation/invite', data=json.dumps(json_data), content_type='application/json') response_json = json.loads(response.data) assert response.status_code != 200 assert not response_json['success'] assert len(self.conv.links) == 2
def setUp(self): neo_config.load_config() neo_config.set_project_variables() neo = NeoAPI(neo_config) self.api = neo.activate_testing() self.user1 = db.session.query(UserModel).filter( UserModel.email == "*****@*****.**").first() if self.user1 is None: self.user1 = UserModel(email="*****@*****.**", password="******", first_name="firstname", last_name="lastname", birthday="1995-12-12") self.user2 = db.session.query(UserModel).filter( UserModel.email == "*****@*****.**").first() if self.user2 is None: self.user2 = UserModel(email="*****@*****.**", password="******", first_name="firstname", last_name="lastname", birthday="1111-11-11") self.circle = Circle(name="TestMessageCreate") self.linkCircle = UserToCircle(user=self.user1, circle=self.circle) self.linkCircle2 = UserToCircle(user=self.user2, circle=self.circle) self.conversation = Conversation(circle=self.circle, name="TestMessageCreate") self.linkConversation = UserToConversation( user=self.user1, conversation=self.conversation, privilege="ADMIN") self.linkConversation2 = UserToConversation( user=self.user2, conversation=self.conversation) self.message = Message(link=self.linkConversation, conversation=self.conversation, content="1") self.message2 = Message(link=self.linkConversation, conversation=self.conversation, content="2") self.message3 = Message(link=self.linkConversation2, conversation=self.conversation, content="3") db.session.commit() self.token1 = authenticate_user(self.api, self.user1, "test") self.token2 = authenticate_user(self.api, self.user2, "test") self.tokenAdmin = authenticate_user(self.api, "*****@*****.**", "PapieNeo2019")
def setUp(self): neo_config.load_config() neo_config.set_project_variables() neo = NeoAPI(neo_config) self.api = neo.activate_testing() self.user1 = db.session.query(UserModel).filter( UserModel.email == "*****@*****.**").first() if self.user1 is None: self.user1 = UserModel(email="*****@*****.**", password="******", first_name="firstname", last_name="lastname", birthday="1995-12-12") self.user2 = db.session.query(UserModel).filter( UserModel.email == "*****@*****.**").first() if self.user2 is None: self.user2 = UserModel(email="*****@*****.**", password="******", first_name="firstname", last_name="lastname", birthday="1111-11-11") self.circle = Circle(name="Mamie") self.linkCircle = UserToCircle(user=self.user1, circle=self.circle) self.linkCircle2 = UserToCircle(user=self.user2, circle=self.circle) self.conversation = Conversation(circle=self.circle, device_access=True) self.linkConversation = UserToConversation( user=self.user1, conversation=self.conversation, privilege="ADMIN") self.message = Message() self.message.conversation = self.conversation self.message.link = self.linkConversation self.media = Media('test', '.txt', 'test.txt') self.media.message = self.message self.device = Device(name="Papie") self.device.circle = self.circle self.device_password = self.device.get_pre_activation_password() self.device.activate(self.device.key) db.session.commit() self.token1 = authenticate_user(self.api, self.user1, "test") self.token2 = authenticate_user(self.api, self.user2, "test") self.tokenAdmin = authenticate_user(self.api, "*****@*****.**", "PapieNeo2019") self.tokenDevice = authenticate_device(self.api, self.device, self.device_password)
def first_message_to_user(content, email, circle_id, user): try: dest = db.session.query(UserModel).filter( UserModel.email == email).first() circle = db.session.query(Circle).filter( Circle.id == circle_id).first() if dest is None: raise e_message.TargetUserNotFound if circle is None: raise e_circle.CircleNotFound if not circle.has_member(user): raise e_circle.UserNotPartOfCircle if not circle.has_member(dest) and circle.has_member(user): raise e_circle.TargetUserNotPartOfCircle conversation = Conversation() conversation.circle = circle UserToConversation(privilege="ADMIN", user=user, conversation=conversation) UserToConversation(privilege="STANDARD", user=dest, conversation=conversation) message = Message(content=content["text_message"] if "text_message" in content else "") message.conversation = conversation media_list = [] if "files" in content: for file in content["files"]: media = Media() media.identifier = file MessageToMedia(message=message, media=media) db.session.commit() media_list.append(media.get_simple_content()) db.session.commit() sockets.notify_user(dest, False, 'conversation', { "conversation_id": conversation.id, "event": 'invite' }) message.conversation.mobile_notification( title="Conversation", body="Nouvelle conversation avec " + user.first_name) info_sender = "[" + conversation.name + "] " + user.first_name + " : " try: messenger_conversation_model_send( user.id, conversation, info_sender + message.text_content) except Exception: pass try: hangout_conversation_model_send(user.id, conversation, info_sender + message.text_content) except Exception: pass response = { "data": { "success": True, 'media_list': media_list, 'message_id': message.id, 'conversation_id': message.conversation_id }, "status_code": 200 } except (e_message.MessageException, e_circle.CircleException) as exception: response = { "data": { "success": False, "message": exception.message }, "status_code": exception.status_code } return response
def init_default_content(p1, p2): user = db.session.query(User).filter( User.email == "*****@*****.**").first() user2 = db.session.query(User).filter( User.email == "*****@*****.**").first() if user is None and user2 is None: user = User(email="*****@*****.**", password=p1, first_name="user1", last_name="beta", birthday="2019-09-05") user2 = User(email="*****@*****.**", password=p2, first_name="user2", last_name="beta", birthday="2019-09-05") circle = Circle("Cercle Beta 1") db.session.commit() UserToCircle(user=user, circle=circle, privilege="ADMIN") UserToCircle(user=user2, circle=circle) db.session.commit() device = db.session.query(Device).filter( Device.username == "device1").first() if device is None: device = Device(name="Device beta 1") device.circle = circle device.username = "******" device.set_password("test") device.activate(device.key) db.session.commit() if len(circle.conversations) == 0: conversation = Conversation(device_access=True, name="Conversation avec device", circle=circle) conversation2 = Conversation(device_access=False, name="Conversation sans device", circle=circle) db.session.commit() if len(user.conversation_links) == 0: cl1 = UserToConversation(privilege="ADMIN", user=user, conversation=conversation) cl2 = UserToConversation(user=user2, conversation=conversation) db.session.commit() Message(content="Message conversation avec device from user1", link=cl1, conversation=conversation) Message(content="Message conversation avec device from user2", link=cl2, conversation=conversation) message3 = Message( content="Message conversation avec device from device", is_user=False, conversation=conversation) message3.device = device db.session.commit() if len(user2.conversation_links) == 0: cl3 = UserToConversation(privilege="ADMIN", user=user, conversation=conversation2) cl4 = UserToConversation(user=user2, conversation=conversation2) db.session.commit() Message(content="Message conversation sans device from user1", link=cl3, conversation=conversation2) Message(content="Message conversation sans device from user2", link=cl4, conversation=conversation2) db.session.commit()