示例#1
0
class TestUserPermission(TestCase):
    def setUp(self):
        random_name = str(uuid1())
        self.user1 = User(username=random_name, email=random_name + '@m.ru')
        self.user1.set_password('12345678')
        self.user1.save()

        random_name = str(uuid1())
        self.user2 = User(username=random_name, email=random_name + '@m.ru')
        self.user2.set_password('12345678')
        self.user2.save()

    def test_send_request_for_relationship(self):
        self.user1.accept(self.user2)

        self.assertEqual(self.user1.check_relationship(self.user2),
                         RELATIONSHIP_REQUEST_HAS_SENT)
        self.assertEqual(self.user2.check_relationship(self.user1),
                         RELATIONSHIP_WAITING_FOR_ACCEPT)

    def test_cancel_own_send_request_for_relationship(self):
        self.user1.accept(self.user2)

        self.assertEqual(self.user1.check_relationship(self.user2),
                         RELATIONSHIP_REQUEST_HAS_SENT)
        self.assertEqual(self.user2.check_relationship(self.user1),
                         RELATIONSHIP_WAITING_FOR_ACCEPT)

        self.user1.cancel(self.user2)

        self.assertEqual(self.user1.check_relationship(self.user2),
                         NO_RELATIONSHIP)
        self.assertEqual(self.user2.check_relationship(self.user1),
                         NO_RELATIONSHIP)

    def test_cancel_foreign_send_request_for_relationship(self):
        self.user1.accept(self.user2)

        self.assertEqual(self.user1.check_relationship(self.user2),
                         RELATIONSHIP_REQUEST_HAS_SENT)
        self.assertEqual(self.user2.check_relationship(self.user1),
                         RELATIONSHIP_WAITING_FOR_ACCEPT)

        self.user2.cancel(self.user1)

        self.assertEqual(self.user1.check_relationship(self.user2),
                         NO_RELATIONSHIP)
        self.assertEqual(self.user2.check_relationship(self.user1),
                         NO_RELATIONSHIP)

    def test_add_to_friends(self):
        self.user1.accept(self.user2)
        self.user2.accept(self.user1)

        self.assertEqual(self.user1.check_relationship(self.user2),
                         RELATIONSHIP_FRIENDS)
        self.assertEqual(self.user2.check_relationship(self.user1),
                         RELATIONSHIP_FRIENDS)

        self.assertEqual(self.user1.get_friends()[0].username,
                         self.user2.username)
        self.assertEqual(self.user2.get_friends()[0].username,
                         self.user1.username)

    def test_remove_from_friends(self):
        self.user1.accept(self.user2)
        self.user2.accept(self.user1)

        self.user1.cancel(self.user2)

        self.assertEqual(self.user1.check_relationship(self.user2),
                         NO_RELATIONSHIP)
        self.assertEqual(self.user2.check_relationship(self.user1),
                         NO_RELATIONSHIP)
示例#2
0
class TestUserPostViews(TestCase):
    def setUp(self):
        random_name = str(uuid1())
        self.user = User(username=random_name, email=random_name + '@m.ru', is_active=True)
        self.user.set_password('12345678')
        self.user.save()

        random_name = str(uuid1())
        self.friend = User(username=random_name, email=random_name + '@m.ru', is_active=True)
        self.friend.set_password('12345678')
        self.friend.save()

    def test_create_post_from_mainpage(self):
        post_content = "test_create_post_from_mainpage"
        client = Client()
        client.login(username=self.user.username, password='******')
        response = client.post(reverse('posts:mainpage'), {'content': post_content})
        self.assertEqual(response.status_code, 200)
        post_from_db = Post.objects.get(user=self.user)
        self.assertEqual(post_from_db.content, post_content)

    def test_user_post_processing_action_get_posts(self):
        post_content = "test_user_post_processing_action_get_posts"
        client = Client()
        client.login(username=self.user.username, password='******')
        response = client.post(reverse('posts:mainpage'), {'content': post_content})
        self.assertEqual(response.status_code, 200)
        post_from_db = Post.objects.get(user=self.user)
        # get own posts from server
        post_from_action = client.post(reverse('posts:user_post_processing'), {'action': 'get_posts'}).json()['posts'][0]
        self.assertEqual(post_from_db.content, post_from_action['content'])

    def test_user_post_processing_like(self):
        post_content = "test_user_post_processing_like"
        client = Client()
        client.login(username=self.user.username, password='******')
        response = client.post(reverse('posts:mainpage'), {'content': post_content})
        self.assertEqual(response.status_code, 200)
        post_from_db = Post.objects.get(user=self.user)
        response = client.post(reverse('posts:user_post_processing'), {'action': 'like', 'id': post_from_db.id}).json()
        self.assertTrue(response['result'])
        self.assertEqual(post_from_db.get_rating(), response['likes'])

    def test_user_post_processing_dislike(self):
        post_content = "test_user_post_processing_dislike"
        client = Client()
        client.login(username=self.user.username, password='******')
        response = client.post(reverse('posts:mainpage'), {'content': post_content})
        self.assertEqual(response.status_code, 200)
        post_from_db = Post.objects.get(user=self.user)
        # set like
        response = client.post(reverse('posts:user_post_processing'), {'action': 'like', 'id': post_from_db.id}).json()
        self.assertTrue(response['result'])
        self.assertEqual(post_from_db.get_rating(), response['likes'])
        # remove like
        response = client.post(reverse('posts:user_post_processing'), {'action': 'dislike', 'id': post_from_db.id}).json()
        self.assertTrue(response['result'])
        self.assertEqual(post_from_db.get_rating(), response['likes'])

    def test_user_post_processing_remove(self):
        post_content = "test_user_post_processing_remove"
        client = Client()
        client.login(username=self.user.username, password='******')
        response = client.post(reverse('posts:mainpage'), {'content': post_content})
        self.assertEqual(response.status_code, 200)
        post_from_db = Post.objects.get(user=self.user)
        response = client.post(reverse('posts:user_post_processing'), {'action': 'remove', 'id': post_from_db.id}).json()
        self.assertTrue(response['result'])
        with self.assertRaises(ObjectDoesNotExist):
            Post.objects.get(user=self.user)

    def test_user_news_action_get_news(self):
        post_content = "test_user_news_action_get_news"
        client = Client()
        client.login(username=self.user.username, password='******')
        response = client.post(reverse('posts:mainpage'), {'content': post_content})
        self.assertEqual(response.status_code, 200)
        post_from_db = Post.objects.get(user=self.user)
        # must be friends
        self.friend.accept(self.user)
        self.user.accept(self.friend)
        self.assertEqual(self.user.check_relationship(self.friend), RELATIONSHIP_FRIENDS)
        # login as another user
        client.login(username=self.friend.username, password='******')
        # get news
        post_from_view = client.post(reverse('posts:news'), {'action': 'get_posts'}).json()['posts'][0]
        self.assertEqual(post_from_db.content, post_from_view['content'])
        self.assertEqual(post_from_db.id, post_from_view['id'])

    def test_create_post_from_mainpage_when_content_too_large(self):
        post_content = "1" * (MAX_PAGE_SIZE + 1)
        client = Client()
        client.login(username=self.user.username, password='******')
        response = client.post(reverse('posts:mainpage'), {'content': post_content})
        self.assertEqual(response.status_code, 200)
        self.assertEqual(response.context['post_form'].errors['__all__'][0], 'Not enough space!')
        with self.assertRaises(ObjectDoesNotExist):
            Post.objects.get(user=self.user)

    def test_user_post_processing_remove_on_foreign_post(self):
        post_content = "test_user_post_processing_remove_on_foreign_post"
        client = Client()
        client.login(username=self.user.username, password='******')
        response = client.post(reverse('posts:mainpage'), {'content': post_content})
        self.assertEqual(response.status_code, 200)
        post_from_db = Post.objects.get(user=self.user)

        client.login(username=self.friend.username, password='******')
        response = client.post(reverse('posts:user_post_processing'), {'action': 'remove', 'id': post_from_db.id}).json()
        self.assertFalse(response['result'])
        self.assertEqual(Post.objects.get(user=self.user).content, post_from_db.content)

    def test_user_news_action_get_news_when_users_are_not_friends(self):
        post_content = "test_user_news_action_get_news_when_users_are_not_friends"
        client = Client()
        client.login(username=self.user.username, password='******')
        response = client.post(reverse('posts:mainpage'), {'content': post_content})
        self.assertEqual(response.status_code, 200)
        # check that users are not friends
        self.assertEqual(self.user.check_relationship(self.friend), NO_RELATIONSHIP)
        # login as another user
        client.login(username=self.friend.username, password='******')
        # get news
        post_from_view = client.post(reverse('posts:news'), {'action': 'get_posts'}).json()['posts']
        self.assertEqual(len(post_from_view), 0)
        # check that post exists
        post_from_db = Post.objects.get(user=self.user)
        self.assertEqual(post_from_db.content, post_content)
示例#3
0
class TestChat(ChannelsBaseTestCase):
    password = '******'

    def setUp(self):

        # create two users

        self.user1 = User(username='******', email='*****@*****.**', is_active=True)
        self.user1.set_password(self.password)
        self.user1.save()

        self.user2 = User(username='******', email='*****@*****.**', is_active=True)
        self.user2.set_password(self.password)
        self.user2.save()

        # create relationships between them
        self.user1.accept(self.user2)
        self.user2.accept(self.user1)

    def user1_send_chatmessage_to_user2(self):
        browser = self.selenium
        browser.get(self.live_server_url)
        browser.set_window_size(1920, 1000)
        element = browser.find_element(By.LINK_TEXT, "Log in")
        actions = ActionChains(browser)
        actions.move_to_element(element).perform()
        browser.find_element(By.LINK_TEXT, "Log in").click()
        browser.find_element(By.NAME, "username").click()
        browser.find_element(By.NAME, "username").send_keys(self.user1.username)
        browser.find_element(By.NAME, "password").send_keys(self.password)
        browser.find_element(By.CSS_SELECTOR, ".btn").click()
        browser.find_element(By.ID, "friends_main_menu_button").click()
        browser.find_element(By.LINK_TEXT, "test_ui_2 ()").click()
        browser.find_element(By.LINK_TEXT, "Chat").click()
        browser.find_element(By.ID, "send_message_form_text").click()
        browser.find_element(By.ID, "send_message_form_text").send_keys("Hi! This is test message for user id2")
        browser.find_element(By.ID, "send_message_btn").click()
        mymessage = browser.find_element(By.CLASS_NAME, "chat__table_message_written_by_owner")
        assert mymessage.text == "Hi! This is test message for user id2"
        browser.get(self.live_server_url + reverse("profile:logout"))

    def user2_check_chatmessage_from_user1(self):
        browser = self.selenium
        browser.get(self.live_server_url)
        browser.set_window_size(1920, 1020)
        element = browser.find_element(By.LINK_TEXT, "Log in")
        actions = ActionChains(browser)
        actions.move_to_element(element).perform()
        browser.find_element(By.LINK_TEXT, "Log in").click()
        browser.find_element(By.NAME, "username").click()
        browser.find_element(By.NAME, "username").send_keys(self.user2.username)
        browser.find_element(By.NAME, "password").send_keys(self.password)
        browser.find_element(By.CSS_SELECTOR, ".btn").click()
        browser.find_element(By.ID, "friends_main_menu_button").click()
        browser.find_element(By.LINK_TEXT, "test_ui_1 ()").click()
        browser.find_element(By.LINK_TEXT, "Chat").click()
        browser.find_element(By.ID, "send_message_form_text").click()
        browser.find_element(By.ID, "send_message_form_text").send_keys("Hi! This is test message for user id1")
        browser.find_element(By.ID, "send_message_btn").click()
        # check message from user1
        all_messages = browser.find_elements(By.CLASS_NAME, "chat__table_message_content")
        user1message = list(filter(lambda m: "chat__table_message_written_by_owner" not in m.get_attribute("class"),
                                   all_messages))[0]

        assert user1message.text == "Hi! This is test message for user id2"
        # check own message
        mymessage = browser.find_element(By.CLASS_NAME, "chat__table_message_written_by_owner")
        assert mymessage.text == "Hi! This is test message for user id1"
        browser.get(self.live_server_url + reverse("profile:logout"))

    def test_chat_messaging(self):
        self.user1_send_chatmessage_to_user2()
        self.user2_check_chatmessage_from_user1()