Commit 890e9a256d239445a4547712b8f64b6433e83d36
Exists in
master
and in
4 other branches
Merge pull request #120 from colab/increase_code_coverage
Increase code coverage
Showing
7 changed files
with
208 additions
and
2 deletions
Show diff stats
.coveragerc
| @@ -0,0 +1,19 @@ | @@ -0,0 +1,19 @@ | ||
| 1 | +import urllib2 | ||
| 2 | +from mock import patch, Mock | ||
| 3 | + | ||
| 4 | +from django.test import TestCase | ||
| 5 | + | ||
| 6 | +from ..utils.validators import validate_social_account | ||
| 7 | + | ||
| 8 | + | ||
| 9 | +class TestValidators(TestCase): | ||
| 10 | + | ||
| 11 | + @patch('urllib2.urlopen', | ||
| 12 | + side_effect=urllib2.HTTPError(500, "test", 1, 2, None)) | ||
| 13 | + def test_validate_social_account_with_fake_account(self, urlopen_mock): | ||
| 14 | + self.assertFalse(validate_social_account('john-fake', | ||
| 15 | + 'http://twitter.com')) | ||
| 16 | + | ||
| 17 | + @patch('urllib2.urlopen', return_value=Mock(code=200)) | ||
| 18 | + def test_validate_social_account(self, urlopen_mock): | ||
| 19 | + self.assertTrue(validate_social_account('john', 'http://twitter.com')) |
colab/home/context_processors.py
| @@ -16,7 +16,7 @@ def ribbon(request): | @@ -16,7 +16,7 @@ def ribbon(request): | ||
| 16 | if not enabled: | 16 | if not enabled: |
| 17 | return {'ribbon': False} | 17 | return {'ribbon': False} |
| 18 | 18 | ||
| 19 | - url = 'http://beta.softwarepublico.gov.br/gitlab/softwarepublico/colab' | 19 | + url = 'http://github.com/colab/colab' |
| 20 | text = _('Fork me!') | 20 | text = _('Fork me!') |
| 21 | 21 | ||
| 22 | return { | 22 | return { |
| @@ -0,0 +1,135 @@ | @@ -0,0 +1,135 @@ | ||
| 1 | +from mock import Mock | ||
| 2 | + | ||
| 3 | +from django.test import TestCase, Client | ||
| 4 | +from django.core.cache import cache | ||
| 5 | + | ||
| 6 | +from colab.plugins.templatetags import plugins | ||
| 7 | +from colab.accounts.models import User | ||
| 8 | + | ||
| 9 | + | ||
| 10 | +class PluginsMenuTest(TestCase): | ||
| 11 | + | ||
| 12 | + def setUp(self): | ||
| 13 | + self.user = self.create_user() | ||
| 14 | + self.client = Client() | ||
| 15 | + cache.clear() | ||
| 16 | + | ||
| 17 | + def tearDown(self): | ||
| 18 | + cache.clear() | ||
| 19 | + self.client.logout() | ||
| 20 | + | ||
| 21 | + def create_user(self): | ||
| 22 | + user = User() | ||
| 23 | + user.username = "USERtestCoLaB" | ||
| 24 | + user.set_password("123colab4") | ||
| 25 | + user.email = "usertest@colab.com.br" | ||
| 26 | + user.id = 1 | ||
| 27 | + user.first_name = "USERtestCoLaB" | ||
| 28 | + user.last_name = "COLAB" | ||
| 29 | + user.save() | ||
| 30 | + | ||
| 31 | + return user | ||
| 32 | + | ||
| 33 | + def authenticate_user(self): | ||
| 34 | + self.client.login(username=self.user.username, | ||
| 35 | + password="123colab4") | ||
| 36 | + | ||
| 37 | + def test_plugins_menu_without_menu_urls(self): | ||
| 38 | + self.authenticate_user() | ||
| 39 | + plugin_1 = {'menu_title': 'myTitle', 'menu_urls': []} | ||
| 40 | + | ||
| 41 | + test_context = {'user': self.user, | ||
| 42 | + 'plugins': {'plugin_1': plugin_1}} | ||
| 43 | + | ||
| 44 | + menu = plugins.plugins_menu(test_context) | ||
| 45 | + | ||
| 46 | + self.assertEquals(menu.strip(), "") | ||
| 47 | + | ||
| 48 | + def test_plugins_menu_with_1_menu_urls(self): | ||
| 49 | + self.authenticate_user() | ||
| 50 | + link = 'http://url' | ||
| 51 | + title = 'myTitle' | ||
| 52 | + plugin_1 = {'menu_title': title, | ||
| 53 | + 'menu_urls': [{'url': link, 'display': 'LRU'}]} | ||
| 54 | + | ||
| 55 | + test_context = {'user': self.user, | ||
| 56 | + 'plugins': {'plugin_1': plugin_1}} | ||
| 57 | + | ||
| 58 | + menu = plugins.plugins_menu(test_context) | ||
| 59 | + | ||
| 60 | + self.assertIn(link, menu) | ||
| 61 | + self.assertIn(title, menu) | ||
| 62 | + | ||
| 63 | + def test_plugins_menu_with_many_menu_urls(self): | ||
| 64 | + self.authenticate_user() | ||
| 65 | + | ||
| 66 | + link1 = 'http://url1' | ||
| 67 | + title1 = 'myTitle1' | ||
| 68 | + display1 = 'LRU1' | ||
| 69 | + link2 = 'http://url2' | ||
| 70 | + display2 = 'LRU2' | ||
| 71 | + | ||
| 72 | + plugin_1 = {'menu_title': title1, | ||
| 73 | + 'menu_urls': [{'url': link1, 'display': display1}, | ||
| 74 | + {'url': link2, 'display': display2}]} | ||
| 75 | + | ||
| 76 | + test_context = {'user': self.user, | ||
| 77 | + 'plugins': {'plugin_1': plugin_1}} | ||
| 78 | + | ||
| 79 | + menu = plugins.plugins_menu(test_context) | ||
| 80 | + | ||
| 81 | + self.assertIn(link1, menu) | ||
| 82 | + self.assertIn(title1, menu) | ||
| 83 | + self.assertIn(display1, menu) | ||
| 84 | + self.assertIn(link2, menu) | ||
| 85 | + self.assertIn(display2, menu) | ||
| 86 | + | ||
| 87 | + def test_plugins_menu_with_multiple_plugins(self): | ||
| 88 | + self.authenticate_user() | ||
| 89 | + | ||
| 90 | + link1 = 'http://url1' | ||
| 91 | + title1 = 'myTitle1' | ||
| 92 | + display1 = 'LRU1' | ||
| 93 | + link2 = 'http://url2' | ||
| 94 | + display2 = 'LRU2' | ||
| 95 | + | ||
| 96 | + plugin_1 = {'menu_title': title1, | ||
| 97 | + 'menu_urls': [{'url': link1, 'display': display1}, | ||
| 98 | + {'url': link2, 'display': display2}]} | ||
| 99 | + | ||
| 100 | + title2 = 'myTitle2' | ||
| 101 | + plugin_2 = {'menu_title': title2, | ||
| 102 | + 'menu_urls': []} | ||
| 103 | + | ||
| 104 | + test_context = {'user': self.user, | ||
| 105 | + 'plugins': {'plugin_1': plugin_1, | ||
| 106 | + 'plugin_2': plugin_2}} | ||
| 107 | + | ||
| 108 | + menu = plugins.plugins_menu(test_context) | ||
| 109 | + | ||
| 110 | + self.assertIn(link1, menu) | ||
| 111 | + self.assertIn(title1, menu) | ||
| 112 | + self.assertIn(display1, menu) | ||
| 113 | + self.assertIn(link2, menu) | ||
| 114 | + self.assertIn(display2, menu) | ||
| 115 | + self.assertNotIn(title2, menu) | ||
| 116 | + | ||
| 117 | + class ColabUrlMock(Mock): | ||
| 118 | + def auth(self): | ||
| 119 | + return True | ||
| 120 | + | ||
| 121 | + def test_plugins_menu_with_inactivate_user(self): | ||
| 122 | + self.user.is_active = False | ||
| 123 | + self.user.save() | ||
| 124 | + | ||
| 125 | + self.authenticate_user() | ||
| 126 | + title = 'myTitle' | ||
| 127 | + plugin_1 = {'menu_title': title, | ||
| 128 | + 'menu_urls': [self.ColabUrlMock()]} | ||
| 129 | + | ||
| 130 | + test_context = {'user': self.user, | ||
| 131 | + 'plugins': {'plugin_1': plugin_1}} | ||
| 132 | + | ||
| 133 | + menu = plugins.plugins_menu(test_context) | ||
| 134 | + | ||
| 135 | + self.assertEquals("", menu.strip()) |
| @@ -0,0 +1,22 @@ | @@ -0,0 +1,22 @@ | ||
| 1 | +from django.test import TestCase | ||
| 2 | +from django.test.client import RequestFactory | ||
| 3 | + | ||
| 4 | +from ..views import ColabProxyView | ||
| 5 | +from colab.accounts.models import User | ||
| 6 | + | ||
| 7 | + | ||
| 8 | +class ViewsTest(TestCase): | ||
| 9 | + | ||
| 10 | + def setUp(self): | ||
| 11 | + self.view = ColabProxyView() | ||
| 12 | + self.factory = RequestFactory() | ||
| 13 | + self.user = User.objects.create_user( | ||
| 14 | + username='john', email='john@test.org', password='123', | ||
| 15 | + first_name='John', last_name='John') | ||
| 16 | + | ||
| 17 | + def test_dispatch_without_app_label(self): | ||
| 18 | + request = self.factory.get('/') | ||
| 19 | + request.user = self.user | ||
| 20 | + | ||
| 21 | + with self.assertRaises(NotImplementedError): | ||
| 22 | + self.view.dispatch(request, '/') |
| @@ -0,0 +1,28 @@ | @@ -0,0 +1,28 @@ | ||
| 1 | +from django.test import TestCase | ||
| 2 | + | ||
| 3 | +from ..utils.blocks import EmailBlock | ||
| 4 | + | ||
| 5 | + | ||
| 6 | +class TestEmailBlock(TestCase): | ||
| 7 | + | ||
| 8 | + def setUp(self): | ||
| 9 | + self.email_block = EmailBlock() | ||
| 10 | + | ||
| 11 | + def test_html2text_without_br_tag(self): | ||
| 12 | + html = '<a>Hello, world!</a>' | ||
| 13 | + text = self.email_block._html2text(html) | ||
| 14 | + | ||
| 15 | + self.assertEquals(text, 'Hello, world!') | ||
| 16 | + | ||
| 17 | + def test_html2text_with_br_tag(self): | ||
| 18 | + html = '<a>Hello, world</a>!<br><p>Test with br tag</p>!' | ||
| 19 | + text = self.email_block._html2text(html) | ||
| 20 | + | ||
| 21 | + self.assertEquals(text, 'Hello, world!\nTest with br tag!') | ||
| 22 | + | ||
| 23 | + def test_mark_links(self): | ||
| 24 | + html = 'http://test.org/' | ||
| 25 | + text = self.email_block._mark_links(html) | ||
| 26 | + | ||
| 27 | + self.assertEquals(text, '<a target="_blank" href=' + | ||
| 28 | + '"http://test.org/">http://test.org/</a>') |
colab/super_archives/utils/blocks.py
| @@ -25,7 +25,7 @@ class EmailBlock(list): | @@ -25,7 +25,7 @@ class EmailBlock(list): | ||
| 25 | 25 | ||
| 26 | def _html2text(self, text): | 26 | def _html2text(self, text): |
| 27 | if RE_WRAPPED_BY_HTML.match(text.strip()): | 27 | if RE_WRAPPED_BY_HTML.match(text.strip()): |
| 28 | - return html2text(text) | 28 | + return html2text(text).strip() |
| 29 | 29 | ||
| 30 | text, n = RE_BR_TO_LINEBREAK.subn('\n', text) | 30 | text, n = RE_BR_TO_LINEBREAK.subn('\n', text) |
| 31 | text = strip_tags(text) | 31 | text = strip_tags(text) |