Skip to content
This repository was archived by the owner on Dec 17, 2019. It is now read-only.

Commit 837c077

Browse files
author
Alejandro Gómez
committed
test Label validation errors
1 parent 8972834 commit 837c077

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

pygithub3/requests/issues/labels.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,13 @@ class Create(Request):
1818
'required': ('name', 'color' )
1919
}
2020

21-
def validate_color(color):
22-
color = color.get('color', '')
21+
def clean_body(self):
22+
color = self.body.get('color', '')
2323
if not Label.is_valid_color(color):
2424
raise ValidationError('colors must have 6 hexadecimal characters, '
2525
'without # in the beggining')
26+
else:
27+
return self.body
2628

2729

2830
class Update(Request):
@@ -33,12 +35,13 @@ class Update(Request):
3335
'schema': ('name', 'color'),
3436
'required': ('name', 'color' )
3537
}
36-
37-
def validate_color(color):
38-
color = color.get('color', '')
38+
def clean_body(self):
39+
color = self.body.get('color', '')
3940
if not Label.is_valid_color(color):
4041
raise ValidationError('colors must have 6 hexadecimal characters, '
4142
'without # in the beggining')
43+
else:
44+
return self.body
4245

4346

4447
class Delete(Request):
@@ -61,6 +64,7 @@ class Add_to_issue(Request):
6164
uri = 'repos/{user}/{repo}/issues/{number}/labels'
6265
resource = Label
6366

67+
6468
class Remove_from_issue(Request):
6569
uri = 'repos/{user}/{repo}/issues/{number}/labels/{name}'
6670
resource = Label

pygithub3/requests/repos/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ class List_milestones(Request):
8989

9090
uri = 'repos/{user}/{repo}/milestones'
9191
resource = Milestone
92-
# TODO: validate
9392
body_schema = {
9493
'schema': ('state', 'sort', 'direction'),
9594
'required': ()

pygithub3/resources/issues.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Label(Resource):
1111
@staticmethod
1212
def is_valid_color(color):
1313
valid_color = re.compile(r'[0-9abcdefABCDEF]{6}')
14-
match = valid_color(color)
14+
match = valid_color.match(color)
1515
if match is None:
1616
return False
1717
return match.start() == 0 and match.end() == len(color)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env python
2+
# -*- encoding: utf-8 -*-
3+
4+
from unittest import TestCase
5+
6+
from pygithub3.resources.issues import Label
7+
8+
9+
class TestLabel(TestCase):
10+
def test_is_valid_color(self):
11+
valid_colors = ['BADA55', 'FFFFFF', '45DFCA']
12+
for color in valid_colors:
13+
self.assertTrue(Label.is_valid_color(color))
14+
15+
invalid_colors = ['BDA55', '#FFAABB', 'FFf']
16+
for color in invalid_colors:
17+
self.assertFalse(Label.is_valid_color(color))

pygithub3/tests/services/test_issues.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import requests
55
from mock import patch, Mock
66

7+
from pygithub3.exceptions import ValidationError
78
from pygithub3.tests.utils.core import TestCase
89
from pygithub3.resources.base import json
910
from pygithub3.services.issues import Issue, Comments, Events, Labels, Milestones
@@ -133,12 +134,33 @@ def test_CREATE(self, request_method):
133134
self.assertEqual(request_method.call_args[0],
134135
('post', _('repos/octocat/Hello-World/labels')))
135136

137+
def test_CREATE_with_invalid_color(self, request_method):
138+
request_method.return_value = mock_response('post')
139+
# invalid color
140+
with self.assertRaises(ValidationError):
141+
args={'user': 'octocat',
142+
'repo': 'Hello-world',
143+
'name': 'bug',
144+
'color': 'FF00',}
145+
self.lb.create(**args)
146+
136147
def test_UPDATE(self, request_method):
137148
request_method.return_value = mock_response('patch')
138149
self.lb.update('octocat', 'Hello-World', 'bug', 'critical', 'FF0000')
139150
self.assertEqual(request_method.call_args[0],
140151
('patch', _('repos/octocat/Hello-World/labels/bug')))
141152

153+
def test_UPDATE_with_invalid_color(self, request_method):
154+
request_method.return_value = mock_response('post')
155+
# invalid color
156+
with self.assertRaises(ValidationError):
157+
args={'user': 'octocat',
158+
'repo': 'Hello-world',
159+
'name': 'bug',
160+
'new_name': 'critical',
161+
'color': 'FF00',}
162+
self.lb.update(**args)
163+
142164
def test_DELETE(self, request_method):
143165
request_method.return_value = mock_response('delete')
144166
self.lb.delete('octocat', 'Hello-World', 'bug')

0 commit comments

Comments
 (0)