/ tests / BadAttributes.py
BadAttributes.py
  1  ############################ Copyrights and license ############################
  2  #                                                                              #
  3  # Copyright 2013 Vincent Jacques <vincent@vincent-jacques.net>                 #
  4  # Copyright 2014 Vincent Jacques <vincent@vincent-jacques.net>                 #
  5  # Copyright 2016 Peter Buckley <dx-pbuckley@users.noreply.github.com>          #
  6  # Copyright 2017 Hugo <hugovk@users.noreply.github.com>                        #
  7  # Copyright 2018 sfdye <tsfdye@gmail.com>                                      #
  8  #                                                                              #
  9  # This file is part of PyGithub.                                               #
 10  # http://pygithub.readthedocs.io/                                              #
 11  #                                                                              #
 12  # PyGithub is free software: you can redistribute it and/or modify it under    #
 13  # the terms of the GNU Lesser General Public License as published by the Free  #
 14  # Software Foundation, either version 3 of the License, or (at your option)    #
 15  # any later version.                                                           #
 16  #                                                                              #
 17  # PyGithub is distributed in the hope that it will be useful, but WITHOUT ANY  #
 18  # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS    #
 19  # FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more #
 20  # details.                                                                     #
 21  #                                                                              #
 22  # You should have received a copy of the GNU Lesser General Public License     #
 23  # along with PyGithub. If not, see <http://www.gnu.org/licenses/>.             #
 24  #                                                                              #
 25  ################################################################################
 26  
 27  from datetime import datetime, timezone
 28  
 29  from dateutil.parser import ParserError
 30  
 31  import github
 32  
 33  from . import Framework
 34  
 35  
 36  # Replay data is forged to simulate bad things returned by Github
 37  class BadAttributes(Framework.TestCase):
 38      def testBadSimpleAttribute(self):
 39          user = self.g.get_user("klmitch")
 40          self.assertEqual(
 41              user.created_at,
 42              datetime(2011, 3, 23, 15, 42, 9, tzinfo=timezone.utc),
 43          )
 44  
 45          with self.assertRaises(github.BadAttributeException) as raisedexp:
 46              user.name
 47          self.assertEqual(raisedexp.exception.actual_value, 42)
 48          self.assertEqual(raisedexp.exception.expected_type, str)
 49          self.assertEqual(raisedexp.exception.transformation_exception, None)
 50  
 51      def testBadAttributeTransformation(self):
 52          user = self.g.get_user("klmitch")
 53          self.assertEqual(user.name, "Kevin L. Mitchell")
 54  
 55          with self.assertRaises(github.BadAttributeException) as raisedexp:
 56              user.created_at
 57          self.assertEqual(raisedexp.exception.actual_value, "foobar")
 58          self.assertEqual(raisedexp.exception.expected_type, str)
 59          self.assertEqual(raisedexp.exception.transformation_exception.__class__, ParserError)
 60          self.assertEqual(
 61              raisedexp.exception.transformation_exception.args,
 62              ("Unknown string format: %s", "foobar"),
 63          )
 64  
 65      def testBadTransformedAttribute(self):
 66          user = self.g.get_user("klmitch")
 67          self.assertEqual(user.name, "Kevin L. Mitchell")
 68  
 69          with self.assertRaises(github.BadAttributeException) as raisedexp:
 70              user.updated_at
 71          self.assertEqual(raisedexp.exception.actual_value, 42)
 72          self.assertEqual(raisedexp.exception.expected_type, str)
 73          self.assertEqual(raisedexp.exception.transformation_exception, None)
 74  
 75      def testBadSimpleAttributeInList(self):
 76          hook = self.g.get_hook("activecollab")
 77          self.assertEqual(hook.name, "activecollab")
 78  
 79          with self.assertRaises(github.BadAttributeException) as raisedexp:
 80              hook.events
 81          self.assertEqual(raisedexp.exception.actual_value, ["push", 42])
 82          self.assertEqual(raisedexp.exception.expected_type, [str])
 83          self.assertEqual(raisedexp.exception.transformation_exception, None)
 84  
 85      def testBadAttributeInClassAttribute(self):
 86          repo = self.g.get_repo("klmitch/turnstile")
 87          owner = repo.owner
 88          self.assertEqual(owner.id, 686398)
 89  
 90          with self.assertRaises(github.BadAttributeException) as raisedexp:
 91              owner.avatar_url
 92          self.assertEqual(raisedexp.exception.actual_value, 42)
 93  
 94      def testBadTransformedAttributeInList(self):
 95          commit = self.g.get_repo("klmitch/turnstile", lazy=True).get_commit("38d9082a898d0822b5ccdfd78f3a536e2efa6c26")
 96  
 97          with self.assertRaises(github.BadAttributeException) as raisedexp:
 98              commit.files
 99          self.assertEqual(raisedexp.exception.actual_value, [42])
100          self.assertEqual(raisedexp.exception.expected_type, [dict])
101          self.assertEqual(raisedexp.exception.transformation_exception, None)
102  
103      def testBadTransformedAttributeInDict(self):
104          gist = self.g.get_gist("6437766")
105  
106          with self.assertRaises(github.BadAttributeException) as raisedexp:
107              gist.files
108          self.assertEqual(raisedexp.exception.actual_value, {"test.py": 42})
109          self.assertEqual(raisedexp.exception.expected_type, {str: dict})
110          self.assertEqual(raisedexp.exception.transformation_exception, None)
111  
112      def testIssue195(self):
113          hooks = self.g.get_hooks()
114          # We can loop on all hooks as long as we don't access circleci's events attribute
115          self.assertListKeyEqual(
116              hooks,
117              lambda h: h.name,
118              [
119                  "activecollab",
120                  "acunote",
121                  "agilebench",
122                  "agilezen",
123                  "amazonsns",
124                  "apiary",
125                  "apoio",
126                  "appharbor",
127                  "apropos",
128                  "asana",
129                  "backlog",
130                  "bamboo",
131                  "basecamp",
132                  "bcx",
133                  "blimp",
134                  "boxcar",
135                  "buddycloud",
136                  "bugherd",
137                  "bugly",
138                  "bugzilla",
139                  "campfire",
140                  "cia",
141                  "circleci",
142                  "codeclimate",
143                  "codeportingcsharp2java",
144                  "codeship",
145                  "coffeedocinfo",
146                  "conductor",
147                  "coop",
148                  "copperegg",
149                  "cube",
150                  "depending",
151                  "deployhq",
152                  "devaria",
153                  "docker",
154                  "ducksboard",
155                  "email",
156                  "firebase",
157                  "fisheye",
158                  "flowdock",
159                  "fogbugz",
160                  "freckle",
161                  "friendfeed",
162                  "gemini",
163                  "gemnasium",
164                  "geocommit",
165                  "getlocalization",
166                  "gitlive",
167                  "grmble",
168                  "grouptalent",
169                  "grove",
170                  "habitualist",
171                  "hakiri",
172                  "hall",
173                  "harvest",
174                  "hipchat",
175                  "hostedgraphite",
176                  "hubcap",
177                  "hubci",
178                  "humbug",
179                  "icescrum",
180                  "irc",
181                  "irker",
182                  "ironmq",
183                  "ironworker",
184                  "jabber",
185                  "jaconda",
186                  "jeapie",
187                  "jenkins",
188                  "jenkinsgit",
189                  "jira",
190                  "jqueryplugins",
191                  "kanbanery",
192                  "kickoff",
193                  "leanto",
194                  "lechat",
195                  "lighthouse",
196                  "lingohub",
197                  "loggly",
198                  "mantisbt",
199                  "masterbranch",
200                  "mqttpub",
201                  "nma",
202                  "nodejitsu",
203                  "notifo",
204                  "ontime",
205                  "pachube",
206                  "packagist",
207                  "phraseapp",
208                  "pivotaltracker",
209                  "planbox",
210                  "planio",
211                  "prowl",
212                  "puppetlinter",
213                  "pushalot",
214                  "pushover",
215                  "pythonpackages",
216                  "railsbp",
217                  "railsbrakeman",
218                  "rally",
219                  "rapidpush",
220                  "rationaljazzhub",
221                  "rationalteamconcert",
222                  "rdocinfo",
223                  "readthedocs",
224                  "redmine",
225                  "rubyforge",
226                  "scrumdo",
227                  "shiningpanda",
228                  "sifter",
229                  "simperium",
230                  "slatebox",
231                  "snowyevening",
232                  "socialcast",
233                  "softlayermessaging",
234                  "sourcemint",
235                  "splendidbacon",
236                  "sprintly",
237                  "sqsqueue",
238                  "stackmob",
239                  "statusnet",
240                  "talker",
241                  "targetprocess",
242                  "tddium",
243                  "teamcity",
244                  "tender",
245                  "tenxer",
246                  "testpilot",
247                  "toggl",
248                  "trac",
249                  "trajectory",
250                  "travis",
251                  "trello",
252                  "twilio",
253                  "twitter",
254                  "unfuddle",
255                  "web",
256                  "weblate",
257                  "webtranslateit",
258                  "yammer",
259                  "youtrack",
260                  "zendesk",
261                  "zohoprojects",
262              ],
263          )
264          for hook in hooks:
265              if hook.name != "circleci":
266                  hook.events
267  
268          for hook in hooks:
269              if hook.name == "circleci":
270                  with self.assertRaises(github.BadAttributeException) as raisedexp:
271                      hook.events
272          self.assertEqual(
273              raisedexp.exception.actual_value,
274              [
275                  [
276                      "commit_comment",
277                      "create",
278                      "delete",
279                      "download",
280                      "follow",
281                      "fork",
282                      "fork_apply",
283                      "gist",
284                      "gollum",
285                      "issue_comment",
286                      "issues",
287                      "member",
288                      "public",
289                      "pull_request",
290                      "pull_request_review_comment",
291                      "push",
292                      "status",
293                      "team_add",
294                      "watch",
295                  ]
296              ],
297          )
298          self.assertEqual(raisedexp.exception.expected_type, [str])
299          self.assertEqual(raisedexp.exception.transformation_exception, None)