<commit_before>#!/usr/bin/env python
"""
Unit tests for :mod:`clans.client`.

"""

import unittest
from clans.client import PlansConnection
import subprocess
import cookielib
import pdb

TEST_URL = 'http://localhost/~tkb/phplans'

class TestAuth(unittest.TestCase):

    def test_login(self):
        self.pc = PlansConnection(base_url = TEST_URL)
        # we should not be admitted if we give the wrong password
        self.assertFalse(self.pc.plans_login('baldwint', 'wrong_password'))
        # we should be admitted if we provide the correct one
        self.assertTrue(self.pc.plans_login('baldwint', 'password'))
        # once we are logged in, plans_login always returns true
        self.assertTrue(self.pc.plans_login('baldwint', ''))

    def test_cookie(self):
        oreo = cookielib.LWPCookieJar()
        # log in to create a good cookie
        pc = PlansConnection(oreo, base_url = TEST_URL)
        pc.plans_login('baldwint', 'password')
        # end session
        del pc
        # but we should remain logged in if we provide the cookie
        pc = PlansConnection(oreo, base_url = TEST_URL)
        self.assertTrue(pc.plans_login('baldwint', ''))

class LoggedInTestCase(unittest.TestCase):

    def setUp(self):
        self.un = 'baldwint'
        self.pc = PlansConnection(base_url = TEST_URL)
        self.pc.plans_login(self.un, 'password')

class TestEdit(LoggedInTestCase):

    def prepend_and_remove(self, hello):
        orig, hashsum = self.pc.get_edit_text(plus_hash=True)
        result = self.pc.set_edit_text(hello + orig, hashsum)
        self.assertTrue("Plan changed successfully" in str(result))
        # check that plan was actually edited
        new, new_hashsum = self.pc.get_edit_text(plus_hash=True)
        self.assertEqual(new[:len(hello)], hello)
        # try to change back, with wrong hashsum
        result = self.pc.set_edit_text(orig, hashsum)
        self.assertFalse("Plan changed successfully" in str(result))
        # edit again, undoing
        result = self.pc.set_edit_text(orig, new_hashsum)
        self.assertTrue("Plan changed successfully" in str(result))

    def test_plaintext_editing(self):
        #prepend 'hello world' to plan text
        self.prepend_and_remove('hello world!')

    def test_html_editing(self):
        #make sure html is picked up
        self.prepend_and_remove("<tt># 10 11 12 -----------------</tt>")
        self.prepend_and_remove("<b>so excited</b>")
        self.prepend_and_remove("<hr>contact info blah blah<hr>")

    def test_md5(self):
        #TODO
        pass

class PlanChangingTestCase(LoggedInTestCase):
    """
    saves the original plan at the start of tests,
    and restores it at the end.

    """

    def setUp(self):
        super(PlanChangingTestCase, self).setUp()
        self.orig, self.hashnum = self.pc.get_edit_text(plus_hash=True)
        # tests should set hashnum as they go along

    def tearDown(self):
        result = self.pc.set_edit_text(self.orig, self.hashnum)
        self.assertTrue("Plan changed successfully" in str(result))


class TestPlanspeak(PlanChangingTestCase):
    """
    tests that strings entered in the edit field are
    converted properly to html<commit_msg>Wrote tests for MD5 hashing<commit_after>#!/usr/bin/env python
"""
Unit tests for :mod:`clans.client`.

"""

import unittest
from clans.client import PlansConnection
import subprocess
import cookielib
import pdb
import md5

TEST_URL = 'http://localhost/~tkb/phplans'

class TestAuth(unittest.TestCase):

    def test_login(self):
        self.pc = PlansConnection(base_url = TEST_URL)
        # we should not be admitted if we give the wrong password
        self.assertFalse(self.pc.plans_login('baldwint', 'wrong_password'))
        # we should be admitted if we provide the correct one
        self.assertTrue(self.pc.plans_login('baldwint', 'password'))
        # once we are logged in, plans_login always returns true
        self.assertTrue(self.pc.plans_login('baldwint', ''))

    def test_cookie(self):
        oreo = cookielib.LWPCookieJar()
        # log in to create a good cookie
        pc = PlansConnection(oreo, base_url = TEST_URL)
        pc.plans_login('baldwint', 'password')
        # end session
        del pc
        # but we should remain logged in if we provide the cookie
        pc = PlansConnection(oreo, base_url = TEST_URL)
        self.assertTrue(pc.plans_login('baldwint', ''))

class LoggedInTestCase(unittest.TestCase):

    def setUp(self):
        self.un = 'baldwint'
        self.pc = PlansConnection(base_url = TEST_URL)
        self.pc.plans_login(self.un, 'password')

class TestEdit(LoggedInTestCase):

    def prepend_and_remove(self, hello):
        orig, hashsum = self.pc.get_edit_text(plus_hash=True)
        result = self.pc.set_edit_text(hello + orig, hashsum)
        self.assertTrue("Plan changed successfully" in str(result))
        # check that plan was actually edited
        new, new_hashsum = self.pc.get_edit_text(plus_hash=True)
        self.assertEqual(new[:len(hello)], hello)
        # try to change back, with wrong hashsum
        result = self.pc.set_edit_text(orig, hashsum)
        self.assertFalse("Plan changed successfully" in str(result))
        # edit again, undoing
        result = self.pc.set_edit_text(orig, new_hashsum)
        self.assertTrue("Plan changed successfully" in str(result))

    def test_plaintext_editing(self):
        #prepend 'hello world' to plan text
        self.prepend_and_remove('hello world!')

    def test_html_editing(self):
        #make sure html is picked up
        self.prepend_and_remove("<tt># 10 11 12 -----------------</tt>")
        self.prepend_and_remove("<b>so excited</b>")
        self.prepend_and_remove("<hr>contact info blah blah<hr>")

    def test_md5(self):
        plan, server_hashnum = self.pc.get_edit_text(plus_hash=True)
        python_hashnum = md5.md5(plan).hexdigest()
        self.assertEqual(server_hashnum, python_hashnum)

class PlanChangingTestCase(LoggedInTestCase):
    """
    saves the original plan at the start of tests,
    and restores it at the end.

    """

    def setUp(self):
        super(PlanChangingTestCase, self).setUp()
        self.orig, self.hashnum = self.pc.get_edit_text(plus_hash=True)
        # tests should set hashnum as they go along
        # (but sometimes they fail)

    def tearDown(self):
        whatever, ending_hash = self.pc.get_edit_text(plus_hash=True)
        result = self.pc.set_edit_text(self.orig, ending_hash)
        self.assertTrue("Plan changed successfully" in str(result))


class TestPlanspeak(PlanChangingTestCase):
    """
    tests that strings entered in the edit field are
    converted properly to html
{'fraction_non_alphanumeric': 0.10520059435364042, 'fraction_numerical': 0.002674591381872214, 'mean_word_length': 3.0572634116937913, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7517705', 'n_tokens_mistral': 2098, 'n_tokens_neox': 2040, 'n_words': 525}