From d5d91e364dd40d1c76b73b7177f5ab503f10afd5 Mon Sep 17 00:00:00 2001 From: Macartur Sousa Date: Mon, 5 Oct 2015 16:16:41 -0300 Subject: [PATCH] Added test and refactored gitlab_view --- src/colab_spb/fixtures/__init__.py | 0 src/colab_spb/fixtures/colab_spb.json | 30 ++++++++++++++++++++++++++++++ src/colab_spb/templates/gitlab_activity.html | 2 +- src/colab_spb/views.py | 40 ++++++++++++++++++++++++---------------- tests/test_colab_integration.py | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/test_get_list.py | 34 ---------------------------------- 6 files changed, 136 insertions(+), 51 deletions(-) create mode 100644 src/colab_spb/fixtures/__init__.py create mode 100644 tests/test_colab_integration.py delete mode 100644 tests/test_get_list.py diff --git a/src/colab_spb/fixtures/__init__.py b/src/colab_spb/fixtures/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/colab_spb/fixtures/__init__.py diff --git a/src/colab_spb/fixtures/colab_spb.json b/src/colab_spb/fixtures/colab_spb.json index 28019c6..0d730dd 100644 --- a/src/colab_spb/fixtures/colab_spb.json +++ b/src/colab_spb/fixtures/colab_spb.json @@ -379,5 +379,35 @@ }, "model": "super_archives.emailaddress", "pk": 1 +}, +{ + "fields": { + "description": null, + "created_at": "2015-09-29T14:36:58Z", + "user": null, + "identifier": "example_community", + "categories": [], + "name": "example_community" + }, + "model": "colab_noosfero.noosferocommunity", + "pk": 69 +}, +{ + "fields": { + "path": "example_community", + "name": "example_community", + "owner_id": null + }, + "model": "colab_gitlab.gitlabgroup", + "pk": 23 +}, +{ + "fields": { + "mail_list": 1, + "group": 23, + "community": 69 + }, + "model": "colab_spb.communityassociations", + "pk": 2 } ] diff --git a/src/colab_spb/templates/gitlab_activity.html b/src/colab_spb/templates/gitlab_activity.html index d88de84..87ae4eb 100644 --- a/src/colab_spb/templates/gitlab_activity.html +++ b/src/colab_spb/templates/gitlab_activity.html @@ -13,7 +13,7 @@ {% if community_association %} - $.getJSON("{{community_association.repository}}",{limit: {{community_association.activities_limit}}, offset: {{community_association.offset}}},function(msg, e){ + $.getJSON("{{community_association.repository}}",{limit: {{community_association.limit}}, offset: {{community_association.offset}}},function(msg, e){ $tag.html(msg.html); $tag.append("
" + "" + diff --git a/src/colab_spb/views.py b/src/colab_spb/views.py index f0cb721..595346c 100644 --- a/src/colab_spb/views.py +++ b/src/colab_spb/views.py @@ -46,30 +46,38 @@ def mail_list(request): message = ("Não foi possível encontrada lista de discussão" " associada a está comunidade, para mais" " detalhes contate o administrador.") - return HttpResponse(message, status=404) + return HttpResponse(message, status=200) return render(request, 'discussion.html', context) def gitlab_activity(request): community = request.GET.get('community', "") + limit = request.GET.get('limit',7) + offset = request.GET.get('offset',0) context = {} context['message'] = ("Esta comunidade não está associada a" - " nenhum repositório no momento, para mais" - " detalhes contate o administrador") - context['community_association'] = get_community_association(community) + " nenhum repositório no momento, para mais" + " detalhes contate o administrador.") + + association = get_community_association(community, limit, offset) + context['community_association'] = association + return render(request, 'gitlab_activity.html', context) -def get_community_association(community): - if community: - associations = CommunityAssociations.objects.all() - for community_association in associations: - if community_association.community.name in community: - return { 'community': community_association.community.name, - 'repository': community_association.group.url, - 'mailman_list': community_association.mail_list.name, - 'activities_limit': 7, - 'offset': 0, - } - return {} +def get_community_association(community,limit=7,offset=0): + if not community: + return {} + + associations = CommunityAssociations.objects.all() + for community_association in associations: + if community_association.community.name in community: + return { 'community': community_association.community.name, + 'repository': community_association.group.url, + 'mailman_list': community_association.mail_list.name, + 'limit': limit, + 'offset': offset, + } + + return {} diff --git a/tests/test_colab_integration.py b/tests/test_colab_integration.py new file mode 100644 index 0000000..aa1f405 --- /dev/null +++ b/tests/test_colab_integration.py @@ -0,0 +1,81 @@ +# -*- coding: utf-8 -*- + +from django.test import TestCase, Client + + +class SPBTest(TestCase): + + fixtures = ['colab_spb.json'] + + def setUp(self): + super(SPBTest, self).setUp() + self.client = Client() + + def tearDown(self): + pass + + def test_mail_list_without_list(self): + response = self.client.get("/spb/mail_list/?community=") + message = ("Não foi possível encontrada lista de discussão" + " associada a está comunidade, para mais" + " detalhes contate o administrador.") + self.assertEqual(message, response.content) + self.assertEqual(200, response.status_code) + + def test_mail_list_with_list(self): + response = self.client.get("/spb/mail_list/" + "?community=example_community&MAX=5") + self.assertEqual(5, len(response.context[1]['latest'])) + + def test_mail_list_default_MAX(self): + response = self.client.get("/spb/mail_list/" + "?community=example_community") + self.assertEqual(7, len(response.context[1]['latest'])) + + def test_mail_list_invalid_MAX(self): + response = self.client.get("/spb/mail_list/" + "?community=example_community&MAX=") + self.assertEqual(7, len(response.context[1]['latest'])) + + def test_gitlab_community_association_with_invalid_community(self): + response = self.client.get("/spb/gitlab_activity/?community=") + message = ("Esta comunidade não está associada a" + " nenhum repositório no momento, para mais" + " detalhes contate o administrador.") + self.assertIn(message, response.content) + self.assertEqual(dict() ,response.context['community_association']) + self.assertEqual(200, response.status_code) + + def test_gitlab_community_association_with_valid_community(self): + response = self.client.get("/spb/gitlab_activity/" + "?community=example_community") + + result = response.context['community_association'] + + self.assertEqual(type(result), dict) + self.assertEqual(result['community'], 'example_community') + self.assertEqual(result['limit'], 7) + self.assertEqual(result['offset'], 0) + self.assertEqual(result['repository'], + '/gitlab/groups/example_community') + self.assertEqual(result['mailman_list'], 'ListA') + + def test_gitlab_community_association_with_no_default_limit(self): + response = self.client.get("/spb/gitlab_activity/" + "?community=example_community" + "&limit=5") + + result = response.context['community_association'] + + self.assertEqual(type(result), dict) + self.assertEqual(result['limit'], "5") + + + def test_gitlab_community_association_with_no_default_offset(self): + response = self.client.get("/spb/gitlab_activity/" + "?community=example_community" + "&offset=5") + + result = response.context['community_association'] + + self.assertEqual(result['offset'],"5") diff --git a/tests/test_get_list.py b/tests/test_get_list.py deleted file mode 100644 index 02b5ae1..0000000 --- a/tests/test_get_list.py +++ /dev/null @@ -1,34 +0,0 @@ -# -*- coding: utf-8 -*- -from django.test import TestCase, Client - - -class SPBTest(TestCase): - - fixtures = ['colab_spb.json'] - - def setUp(self): - super(SPBTest, self).setUp() - self.client = Client() - - def tearDown(self): - pass - - def test_getlist_without_list(self): - response = self.client.get("/spb/get_list/?list_name=") - message = ("Não foi possível encontrada lista de discussão" - " associada a está comunidade, para mais" - " detalhes contate o administrador.") - self.assertEqual(message, response.content) - self.assertEqual(404, response.status_code) - - def test_getlist_with_list(self): - response = self.client.get("/spb/get_list/?list_name=ListA&MAX=5") - self.assertEqual(5, len(response.context[1]['latest'])) - - def test_getlist_default_MAX(self): - response = self.client.get("/spb/get_list/?list_name=ListA") - self.assertEqual(7, len(response.context[1]['latest'])) - - def test_getlist_invalid_MAX(self): - response = self.client.get("/spb/get_list/?list_name=ListA&MAX=") - self.assertEqual(7, len(response.context[1]['latest'])) -- libgit2 0.21.2