def setUp(self): self.user1 = User(username='******', email='*****@*****.**', is_active=True) self.user1.set_password(self.password) self.user1.save()
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 setUp(self): random_name = str(uuid1()) self.user1 = User(username=random_name, email=random_name + '@m.ru', is_active=True) self.user1.set_password(self.password) self.user1.save() random_name = str(uuid1()) self.user2 = User(username=random_name, email=random_name + '@m.ru', is_active=True) self.user2.set_password(self.password) self.user2.save()
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 setUp(self): random_name = str(uuid1()) email = random_name + '@m.ru' self.user = User(username=random_name, email=email) self.user.set_password('12345678') self.user.firstname = 'user firstname' self.user.lastname = 'user lastname' self.user.patronymic = 'user patronymic' self.user.birth_date = timezone.datetime(year=1986, month=4, day=10) self.user.save()
def setUp(self): random_name = str(uuid1()) img_file, content_type = create_test_image() avatar = SimpleUploadedFile('myavatar.bmp', img_file.read(), content_type) self.user = User(username=random_name, email=random_name + '@m.ru', avatar=avatar) self.user.set_password('12345678') self.user.firstname = 'user firstname' self.user.lastname = 'user lastname' self.user.patronymic = 'user patronymic' self.user.birth_date = timezone.datetime(year=1986, month=4, day=10) self.user.save()
class TestUserPosts(TestCase): def setUp(self): random_name = str(uuid1()) self.user = User(username=random_name, email=random_name + '@m.ru') self.user.set_password('12345678') self.user.save() def test_create_post(self): post_content = 'test_create_post' post = Post(user=self.user, content=post_content) post.save() post_from_db = Post.objects.get(user=self.user) self.assertEqual(post_from_db.content, post_content) self.assertEqual(post_from_db.user, self.user)
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)
class TestUserPostForm(TestCase): def setUp(self): random_name = str(uuid1()) self.user = User(username=random_name, email=random_name + '@m.ru') self.user.save() def test_create_correct_post_by_form(self): user_post_form_data = {'content': 'test content'} user_post_form = PostForm(user_post_form_data, user=self.user) self.assertTrue(user_post_form.is_valid()) def test_create_correct_post_by_form_with_image(self): img_file, content_type = create_test_image() img_dict = { 'image': SimpleUploadedFile('myimage.bmp', img_file.read(), content_type) } user_post_form_data = {'content': 'test content'} user_post_form = PostForm(user_post_form_data, img_dict, user=self.user) self.assertTrue(user_post_form.is_valid()) def test_create_incorrect_anonymous_post_by_form(self): user_post_form_data = {'content': 'test content'} user_post_form = PostForm(user_post_form_data) self.assertFalse(user_post_form.is_valid()) self.assertEqual(user_post_form.errors['__all__'][0], 'Anonymous posts are not allowed') def test_create_incorrect_oversized_post_by_form(self): img_file, content_type = create_test_image(1000) img_dict = { 'image': SimpleUploadedFile('myimage.bmp', img_file.read(), content_type) } user_post_form_data = {'content': 'test content'} user_post_form = PostForm(user_post_form_data, img_dict, user=self.user) self.assertFalse(user_post_form.is_valid()) self.assertEqual(user_post_form.errors['__all__'][0], 'Not enough space!')
class TestUserPasswordUpdateForm(TestCase): def setUp(self): random_name = str(uuid1()) email = random_name + '@m.ru' self.user = User(username=random_name, email=email) self.user.set_password('12345678') self.user.firstname = 'user firstname' self.user.lastname = 'user lastname' self.user.patronymic = 'user patronymic' self.user.birth_date = timezone.datetime(year=1986, month=4, day=10) self.user.save() def test_user_password_update_form(self): newpass = '******' update_form = UserPasswordUpdateForm({'password': newpass, 'password_again': newpass}, instance=self.user) self.assertTrue(update_form.is_valid()) update_form.save() self.user.refresh_from_db() self.assertTrue(self.user.check_password(newpass)) def test_user_password_update_form_when_passwords_are_differ(self): newpass = '******' update_form = UserPasswordUpdateForm({'password': newpass, 'password_again': newpass + "occasional symbols"}, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'Passwords mismatch') def test_user_password_update_form_when_password_is_short(self): newpass = '******' * 7 update_form = UserPasswordUpdateForm({'password': newpass, 'password_again': newpass}, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'Password length must be at least 8 symbols') def test_user_password_update_form_when_password_is_too_large(self): newpass = '******' * (MAX_PAGE_SIZE + 1) update_form = UserPasswordUpdateForm({'password': newpass, 'password_again': newpass}, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'You have only 640Kb for all purposes!')
class TestCaching(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() def test_get_size_caching_is_working(self): fake_size = 777 cache.delete(self.user1.id) size = self.user1.get_size() cache.set(self.user1.id, fake_size) self.assertNotEqual(size, fake_size) self.assertEqual(fake_size, self.user1.get_size())
def test_profile_remove_avatar(self): img_file, content_type = create_test_image() random_name = str(uuid1()) email_addr = random_name + '@m.ru' user = User(username=random_name, email=email_addr, avatar=SimpleUploadedFile('myimage.bmp', img_file.read(), content_type), is_active=True) user.set_password('12345678') user.save() # check before deletion self.assertTrue(os.path.exists(user.avatar.path)) client = Client() client.login(username=user.username, password='******') response = client.post(reverse('profile:my_profile'), {'action': 'remove_avatar'}) self.assertEqual(response.status_code, HTTP_OK) # check after deletion self.assertFalse(os.path.exists(user.avatar.path))
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()
class TestPosts(ChannelsBaseTestCase): password = '******' def setUp(self): self.user1 = User(username='******', email='*****@*****.**', is_active=True) self.user1.set_password(self.password) self.user1.save() def tearDown(self): # remove all posts Post.objects.all().delete() # remove user self.user1.delete() pass def test_post_creation(self): browser = self.selenium browser.get(self.live_server_url) browser.set_window_size(1920, 1000) 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, "id_content").click() browser.find_element(By.ID, "id_content").send_keys("test post") browser.find_element(By.CSS_SELECTOR, "[value='Send Post']").click() assert browser.find_element(By.CSS_SELECTOR, ".wordwrap").text.strip() == "test post" browser.get(self.live_server_url + reverse('profile:logout')) # delete our post from DB Post.objects.filter(content="test post").delete() User.objects.all() def test_post_like_creation(self): browser = self.selenium browser.get(self.live_server_url) browser.set_window_size(1920, 1000) 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, "id_content").click() browser.find_element(By.ID, "id_content").send_keys("test post") browser.find_element(By.CSS_SELECTOR, "[value='Send Post']").click() # click on the heart icon to add like browser.find_element(By.CSS_SELECTOR, ".fa-heart").click() assert browser.find_element(By.CSS_SELECTOR, ".badge").text == "1.0" browser.get(self.live_server_url + reverse('profile:logout')) def test_post_deletion(self): browser = self.selenium browser.get(self.live_server_url) browser.set_window_size(1920, 1000) 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, "id_content").click() browser.find_element(By.ID, "id_content").send_keys("test post") browser.find_element(By.CSS_SELECTOR, "[value='Send Post']").click() # click on the heart icon to add like assert browser.find_element(By.CSS_SELECTOR, ".wordwrap").text.strip() == "test post" # click on trash icon and remove post browser.find_element(By.CSS_SELECTOR, ".fa-trash-alt").click() # if there is a "test post" exists on the page that it's an error with self.assertRaises(NoSuchElementException): browser.find_element(By.CSS_SELECTOR, ".wordwrap") browser.get(self.live_server_url + reverse('profile:logout'))
def setUp(self): self.user = User(username='******', email='*****@*****.**', password='******') self.user.save()
class TestUserResourcesSize(TransactionTestCase): TRANSACTION_TEST_CASE = True def setUp(self): self.user = User(username='******', email='*****@*****.**', password='******') self.user.save() def tearDown(self): Post.objects.all().delete() User.objects.all().delete() def test_base_size(self): # -[ RECORD 1 ]---+------------------- # id | 3 # password | 12345678 # last_login | # is_superuser | f # username | test_username # email | [email protected] # firstname | # lastname | # patronymic | # birth_date | # is_active | f # is_admin | f # avatar | # email_confirmed | f # is_staff | f # avatar_size | 0 size_db = self.user._get_size() self.assertEqual(size_db, 46) def test_base_and_post_size(self): new_post = Post(user=self.user, content='test content', image='test image path', image_size=777) new_post.save() # -[ RECORD 1 ]----------------------------- # id | 1 # content | test content # date | 2020-06-18 12:06:07.451794+00 # image | test image path # user_id | 1 # image_size | 777 size_db = self.user._get_size() self.assertEqual(size_db, 46 + 1 + 12 + 29 + 15 + 1 + 3 + 777) def test_base_and_post_and_postlike(self): new_post = Post(user=self.user, content='test content', image='test image path', image_size=777) new_post.save() new_post.likes.add(self.user) # -[ RECORD 1 ] # id | 1 # post_id | 1 # user_id | 1 size_db = self.user._get_size() self.assertEqual(size_db, 46 + 838 + 3)
class TestFriendsView(TestCase): password = '******' def setUp(self): random_name = str(uuid1()) self.user1 = User(username=random_name, email=random_name + '@m.ru', is_active=True) self.user1.set_password(self.password) self.user1.save() random_name = str(uuid1()) self.user2 = User(username=random_name, email=random_name + '@m.ru', is_active=True) self.user2.set_password(self.password) self.user2.save() def test_view_send_request_for_relationship(self): client = Client() client.login(username=self.user1.username, password=self.password) response = client.post(reverse('friends:user_view', kwargs={'user_id': self.user2.id}), {'action': 'add'}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['relationship_status'], RELATIONSHIP_REQUEST_HAS_SENT) self.user1.refresh_from_db() self.user2.refresh_from_db() 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_view_cancel_own_send_request_for_relationship(self): client = Client() client.login(username=self.user1.username, password=self.password) response = client.post(reverse('friends:user_view', kwargs={'user_id': self.user2.id}), {'action': 'add'}) self.assertEqual(response.status_code, 200) self.user1.refresh_from_db() self.user2.refresh_from_db() self.assertEqual(self.user1.check_relationship(self.user2), RELATIONSHIP_REQUEST_HAS_SENT) self.assertEqual(self.user2.check_relationship(self.user1), RELATIONSHIP_WAITING_FOR_ACCEPT) response = client.post(reverse('friends:my_friends'), {'action': 'cancel', 'user_id': self.user2.id}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['relationship_status'], NO_RELATIONSHIP) self.user1.refresh_from_db() self.user2.refresh_from_db() self.assertEqual(self.user1.check_relationship(self.user2), NO_RELATIONSHIP) self.assertEqual(self.user2.check_relationship(self.user1), NO_RELATIONSHIP) def test_view_cancel_foreign_send_request_for_relationship(self): client = Client() client.login(username=self.user1.username, password=self.password) response = client.post(reverse('friends:user_view', kwargs={'user_id': self.user2.id}), {'action': 'add'}) self.assertEqual(response.status_code, 200) self.user1.refresh_from_db() self.user2.refresh_from_db() self.assertEqual(self.user1.check_relationship(self.user2), RELATIONSHIP_REQUEST_HAS_SENT) self.assertEqual(self.user2.check_relationship(self.user1), RELATIONSHIP_WAITING_FOR_ACCEPT) client.login(username=self.user2.username, password=self.password) response = client.post(reverse('friends:my_friends'), {'action': 'cancel', 'user_id': self.user1.id}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['relationship_status'], NO_RELATIONSHIP) self.user1.refresh_from_db() self.user2.refresh_from_db() 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): client = Client() client.login(username=self.user1.username, password=self.password) response = client.post(reverse('friends:user_view', kwargs={'user_id': self.user2.id}), {'action': 'add'}) self.assertEqual(response.status_code, 200) self.user1.refresh_from_db() self.user2.refresh_from_db() self.assertEqual(self.user1.check_relationship(self.user2), RELATIONSHIP_REQUEST_HAS_SENT) self.assertEqual(self.user2.check_relationship(self.user1), RELATIONSHIP_WAITING_FOR_ACCEPT) client.login(username=self.user2.username, password=self.password) response = client.post(reverse('friends:my_friends'), {'action': 'accept', 'user_id': self.user1.id}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['relationship_status'], RELATIONSHIP_FRIENDS) self.user1.refresh_from_db() self.user2.refresh_from_db() 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): client = Client() client.login(username=self.user1.username, password=self.password) response = client.post(reverse('friends:user_view', kwargs={'user_id': self.user2.id}), {'action': 'add'}) self.assertEqual(response.status_code, 200) self.user1.refresh_from_db() self.user2.refresh_from_db() self.assertEqual(self.user1.check_relationship(self.user2), RELATIONSHIP_REQUEST_HAS_SENT) self.assertEqual(self.user2.check_relationship(self.user1), RELATIONSHIP_WAITING_FOR_ACCEPT) client.login(username=self.user2.username, password=self.password) response = client.post(reverse('friends:my_friends'), {'action': 'accept', 'user_id': self.user1.id}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['relationship_status'], RELATIONSHIP_FRIENDS) self.user1.refresh_from_db() self.user2.refresh_from_db() self.assertEqual(self.user1.check_relationship(self.user2), RELATIONSHIP_FRIENDS) self.assertEqual(self.user2.check_relationship(self.user1), RELATIONSHIP_FRIENDS) response = client.post(reverse('friends:my_friends'), {'action': 'cancel', 'user_id': self.user1.id}) self.assertEqual(response.status_code, 200) self.assertEqual(response.json()['relationship_status'], NO_RELATIONSHIP) self.user1.refresh_from_db() self.user2.refresh_from_db() self.assertEqual(self.user1.check_relationship(self.user2), NO_RELATIONSHIP) self.assertEqual(self.user2.check_relationship(self.user1), NO_RELATIONSHIP)
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)
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)
def setUp(self): random_name = str(uuid1()) self.user = User(username=random_name, email=random_name + '@m.ru') self.user.save()
class TestUserProfileViews(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_login_view(self): client = Client() response = client.post(reverse('profile:login'), {'username': self.user.username, 'password': '******'}, follow=True) # check that user redirected to mainpage path, code = response.redirect_chain[0] self.assertEqual(path, '/') self.assertEqual(code, 302) # compare logged user with user in DB self.assertEqual(response.status_code, HTTP_OK) user_from_db = User.objects.get(pk=self.user.pk) self.assertEqual(user_from_db.id, response.context['user'].id) def test_signup_view(self): random_name = str(uuid1()) email_addr = random_name + '@m.ru' client = Client() response = client.post(reverse('profile:signup'), {'username': random_name, 'password': '******', 'password_again': '12345678', 'email': email_addr }, follow=True) self.assertEqual(response.status_code, HTTP_OK) self.assertIn(b'A confirmation code link has been sent to your email address.', response.content) # check email for activation code activation_mail = mail.outbox[-1] recipient_addr = activation_mail.to self.assertEqual(recipient_addr[0], email_addr) activation_link = re.search(r'\s(http[^\s]*)\s', activation_mail.body).group(1) activation_response = client.get(activation_link, follow=True) # check after activation self.assertEqual(activation_response.status_code, HTTP_OK) self.assertIn(b'<title>My page</title>', activation_response.content) def test_signup_view_when_confirmation_code_is_incorrect(self): random_name = str(uuid1()) email_addr = random_name + '@m.ru' client = Client() response = client.post(reverse('profile:signup'), {'username': random_name, 'password': '******', 'password_again': '12345678', 'email': email_addr }, follow=True) self.assertEqual(response.status_code, HTTP_OK) self.assertIn(b'A confirmation code link has been sent to your email address.', response.content) # check email for activation code activation_mail = mail.outbox[-1] recipient_addr = activation_mail.to self.assertEqual(recipient_addr[0], email_addr) activation_link = re.search(r'\s(http[^\s]*)\s', activation_mail.body).group(1) activation_response = client.get(activation_link + 'wrong') # check after activation self.assertEqual(activation_response.status_code, HTTP_NOT_FOUND) def test_profile_view_when_update_existing_user(self): client = Client() client.login(username=self.user.username, password='******') response = client.post(reverse('profile:my_profile'), {'firstname': 'My First Name', 'lastname': 'My Last Name', 'patronymic': 'My Patronymic', 'password': '******', 'password_again': '87654321', }) self.assertEqual(response.status_code, HTTP_OK) user_from_db = User.objects.get(username=self.user.username) self.assertEqual(user_from_db.firstname, 'My First Name') self.assertEqual(user_from_db.lastname, 'My Last Name') self.assertEqual(user_from_db.patronymic, 'My Patronymic') self.assertTrue(user_from_db.check_password('87654321')) def test_profile_remove_avatar(self): img_file, content_type = create_test_image() random_name = str(uuid1()) email_addr = random_name + '@m.ru' user = User(username=random_name, email=email_addr, avatar=SimpleUploadedFile('myimage.bmp', img_file.read(), content_type), is_active=True) user.set_password('12345678') user.save() # check before deletion self.assertTrue(os.path.exists(user.avatar.path)) client = Client() client.login(username=user.username, password='******') response = client.post(reverse('profile:my_profile'), {'action': 'remove_avatar'}) self.assertEqual(response.status_code, HTTP_OK) # check after deletion self.assertFalse(os.path.exists(user.avatar.path)) def test_password_reset(self): client = Client() response = client.post(reverse('profile:reset_password'), {'email': self.user.email}, follow=True) self.assertEqual(response.status_code, HTTP_OK) self.assertIn(b'We\'ve emailed you instructions for setting your password', response.content) # check email for reset code reset_request_mail = mail.outbox[-1] reset_link = re.search(r'\s(?:http|https):\/\/testserver([^\s]*)\s', reset_request_mail.body).group(1) reset_response = client.get(reset_link, follow=True) path, code = response.redirect_chain[0] # check after resetting password self.assertEqual(reset_response.status_code, HTTP_OK) response = client.post(reset_link, follow=True) self.assertNotContains(response, "The password reset link was invalid") # type new password upd_pass_link = response.redirect_chain[0][0] response = client.post(upd_pass_link, data={'password': '******', 'password_again': 'qwertyuio'}) self.user.refresh_from_db() self.assertFalse(self.user.check_password('12345678')) self.assertTrue(self.user.check_password('qwertyuio')) self.assertTrue(client.login(username=self.user.username, password='******'))
class TestUserUpdateForm(TestCase): def setUp(self): random_name = str(uuid1()) img_file, content_type = create_test_image() avatar = SimpleUploadedFile('myavatar.bmp', img_file.read(), content_type) self.user = User(username=random_name, email=random_name + '@m.ru', avatar=avatar) self.user.set_password('12345678') self.user.firstname = 'user firstname' self.user.lastname = 'user lastname' self.user.patronymic = 'user patronymic' self.user.birth_date = timezone.datetime(year=1986, month=4, day=10) self.user.save() def test_update_basic_user_text_data(self): newfirstname = 'new firstname' newlastname = 'new lastname' newpatronymic = 'new patronymic' newbirth_date = '10.04.1986' oldpass = self.user.password update_form = UserUpdateForm({'firstname': newfirstname, 'lastname': newlastname, 'patronymic': newpatronymic, 'birth_date': newbirth_date}, instance=self.user) update_form.is_valid() self.assertTrue(update_form.is_valid()) update_form.save() self.user.refresh_from_db() self.assertEqual(self.user.firstname, newfirstname) self.assertEqual(self.user.lastname, newlastname) self.assertEqual(self.user.patronymic, newpatronymic) self.assertEqual(self.user.password, oldpass) self.assertEqual(timezone.datetime.strftime(self.user.birth_date, DATE_FORMAT), newbirth_date) def test_update_user_avatar(self): img_file, content_type = create_test_image(100) avatar = {'avatar': SimpleUploadedFile('newavatar.bmp', img_file.read(), content_type)} update_form = UserUpdateForm({}, avatar, instance=self.user) self.assertTrue(update_form.is_valid()) update_form.save() self.assertEqual(avatar['avatar'].size, update_form.cleaned_data['avatar'].size) self.assertEqual('newavatar.bmp', update_form.cleaned_data['avatar'].name) def test_update_user_avatar_when_pic_is_too_large(self): img_file, content_type = create_test_image(3000) avatar = {'avatar': SimpleUploadedFile('newavatar.bmp', img_file.read(), content_type)} update_form = UserUpdateForm({}, avatar, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'You have only 640Kb for all purposes!') def test_update_user_password(self): newpass = '******' update_form = UserUpdateForm({'password': newpass, 'password_again': newpass}, instance=self.user) self.assertTrue(update_form.is_valid()) update_form.save() self.user.refresh_from_db() self.assertTrue(self.user.check_password(newpass)) def test_update_user_password_when_passwords_are_differ(self): newpass = '******' update_form = UserUpdateForm({'password': newpass, 'password_again': newpass + "occasional symbols"}, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'Passwords mismatch') def test_update_user_password_when_password_is_short(self): newpass = '******' * 7 update_form = UserUpdateForm({'password': newpass, 'password_again': newpass}, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'Password length must be at least 8 symbols') def test_update_user_password_when_password_is_too_large(self): newpass = '******' * (MAX_PAGE_SIZE + 1) update_form = UserUpdateForm({'password': newpass, 'password_again': newpass}, instance=self.user) self.assertFalse(update_form.is_valid()) self.assertEqual(update_form.errors['__all__'][0], 'You have only 640Kb for all purposes!')