from selenium.webdriver.common.by import By

from .base import FunctionalTest
from .post_page import PostPage
from .my_posts_page import MyPostsPage


class MyPostsTest(FunctionalTest):

    def test_logged_in_users_posts_are_saved_as_my_posts(self):
        self.create_pre_authenticated_session("disco@test.io")

        self.browser.get(self.live_server_url + '/billboard/')
        post_page = PostPage(self)
        post_page.add_post_line("Reticulate splines")
        post_page.add_post_line("Regurgitate spines")
        first_post_url = self.browser.current_url

        MyPostsPage(self).go_to_my_posts_page("disco@test.io")

        self.wait_for(
            lambda: self.browser.find_element(By.LINK_TEXT, "Reticulate splines")
        )
        self.browser.find_element(By.LINK_TEXT, "Reticulate splines").click()
        self.wait_for(
            lambda: self.assertEqual(self.browser.current_url, first_post_url)
        )

        self.browser.get(self.live_server_url + '/billboard/')
        post_page.add_post_line("Ribbon of death")
        second_post_url = self.browser.current_url

        MyPostsPage(self).go_to_my_posts_page("disco@test.io")
        self.wait_for(
            lambda: self.browser.find_element(By.LINK_TEXT, "Ribbon of death")
        )

        self.browser.find_element(By.CSS_SELECTOR, "#id_logout").click()
        self.wait_for(
            lambda: self.assertEqual(
                self.browser.find_elements(By.LINK_TEXT, "My Posts"),
                [],
            )
        )
