|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- encoding: utf-8 -*- |
| 3 | +# |
| 4 | +# author: Antti Kaihola |
| 5 | + |
| 6 | +from datetime import datetime |
| 7 | +import json |
| 8 | +from mock import MagicMock, Mock, patch |
| 9 | +import unittest |
| 10 | + |
| 11 | +import github3.api |
| 12 | +import github3.handlers.gists |
| 13 | +import github3.handlers.user |
| 14 | +import github3.models |
| 15 | + |
| 16 | + |
| 17 | +GIST_RESPONSE = '{"user":{"gravatar_id":"123","url":"https://api.github.com/users/testuser","avatar_url":"https://secure.gravatar.com/avatar/123?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-1.png","login":"testuser","id":12345},"url":"https://api.github.com/gists/791920","history":[{"version":"deadbeefdeadbeefdeadbeefdeadbeefdeadbeef","url":"https://api.github.com/gists/791920/deadbeefdeadbeefdeadbeefdeadbeefdeadbeef","user":{"gravatar_id":"123","url":"https://api.github.com/users/testuser","avatar_url":"https://secure.gravatar.com/avatar/123?d=https://a248.e.akamai.net/assets.github.com%2Fimages%2Fgravatars%2Fgravatar-1.png","login":"testuser","id":12345},"committed_at":"2011-11-09T08:50:53Z","change_status":{"deletions":0,"additions":1,"total":1}}],"description":"description","created_at":"2011-11-09T08:50:53Z","public":true,"comments":0,"updated_at":"2011-11-09T08:50:53Z","git_pull_url":"git://gist.github.com/791920.git","forks":[],"git_push_url":"[email protected]:791920.git","html_url":"https://gist.github.com/791920","id":"791920","files":{"filename.ext":{"raw_url":"https://gist.github.com/raw/791920/badafadacadafadabadacadafadabadabadacada/filename.ext","type":"text/plain","content":"content","size":7,"language":null,"filename":"filename.ext"}}}' |
| 18 | + |
| 19 | + |
| 20 | +class GistsTestCase(unittest.TestCase): |
| 21 | + def test_create_gist(self): |
| 22 | + """The HTTP request for creating a gist is correct""" |
| 23 | + g = github3.api.Github() |
| 24 | + g.session.auth = ('testuser', 'password') |
| 25 | + u = github3.handlers.user.AuthUser(g) |
| 26 | + gists = github3.handlers.gists.Gist(g) |
| 27 | + OpenerDirector = MagicMock(name='OpenerDirector') |
| 28 | + opener = OpenerDirector.return_value |
| 29 | + response = opener.open.return_value |
| 30 | + response.read.return_value = GIST_RESPONSE |
| 31 | + response.code = 201 |
| 32 | + |
| 33 | + with patch('urllib2.OpenerDirector', OpenerDirector): |
| 34 | + |
| 35 | + gist = gists.create_gist( |
| 36 | + 'description', |
| 37 | + files={'filename.ext': {'content': 'content'}}) |
| 38 | + |
| 39 | + request = opener.open.call_args[0][0] |
| 40 | + self.assertEqual(request.method, 'POST') |
| 41 | + self.assertEqual(request.get_full_url(), |
| 42 | + 'https://api.github.com/gists?per_page=100') |
| 43 | + self.assertEqual(request.headers['Authorization'], |
| 44 | + 'Basic dGVzdHVzZXI6cGFzc3dvcmQ=') |
| 45 | + self.assertEqual(json.loads(request.data), |
| 46 | + {u'description': u'description', |
| 47 | + u'files': {u'filename.ext': {u'content': u'content'}}, |
| 48 | + u'public': True}) |
| 49 | + |
| 50 | + |
| 51 | +class GistHandlerTestCase(unittest.TestCase): |
| 52 | + def test_response_conversion(self): |
| 53 | + """A gist response is decoded correctly to a Gist object""" |
| 54 | + g = github3.api.Github() |
| 55 | + handler = github3.handlers.gists.Gist(g) |
| 56 | + converter = handler._get_converter() |
| 57 | + converter.inject(github3.models.Gist) |
| 58 | + |
| 59 | + gist = converter.loads(json.loads(GIST_RESPONSE)) |
| 60 | + |
| 61 | + self.assertEqual( |
| 62 | + {filename: value.__dict__ |
| 63 | + for filename, value in gist.files.iteritems()}, |
| 64 | + {u'filename.ext': { |
| 65 | + 'content': u'content', |
| 66 | + 'filename': u'filename.ext', |
| 67 | + 'raw_url': (u'https://gist.github.com/' |
| 68 | + u'raw/791920/' |
| 69 | + u'badafadacadafadabadacadafadabadabadacada/' |
| 70 | + u'filename.ext'), |
| 71 | + 'size': 7, |
| 72 | + 'type': u'text/plain'}}) |
| 73 | + self.assertEqual(gist.description, u'description') |
| 74 | + self.assertEqual(gist.url, u'https://api.github.com/gists/791920') |
| 75 | + self.assertEqual(gist.created_at, datetime(2011, 11, 9, 8, 50, 53)) |
| 76 | + self.assertEqual(gist.html_url, u'https://gist.github.com/791920') |
| 77 | + self.assertEqual(gist.public, True) |
| 78 | + self.assertEqual( |
| 79 | + gist.user.__dict__, |
| 80 | + {'avatar_url': (u'https://secure.gravatar.com/avatar/123' |
| 81 | + u'?d=https://a248.e.akamai.net/' |
| 82 | + u'assets.github.com%2Fimages%2Fgravatars' |
| 83 | + u'%2Fgravatar-1.png'), |
| 84 | + 'id': 12345, |
| 85 | + 'login': u'testuser', |
| 86 | + 'url': u'https://api.github.com/users/testuser'}) |
| 87 | + self.assertEqual(gist.git_pull_url, u'git://gist.github.com/791920.git') |
| 88 | + self. assertEqual( gist. git_push_url, u'[email protected]:791920.git') |
| 89 | + self.assertEqual(gist.id, u'791920') |
| 90 | + self.assertEqual(len(gist.history), 1) |
| 91 | + h = gist.history[0] |
| 92 | + self.assertEqual(h.change_status.__dict__, {'additions': 1, 'total': 1}) |
| 93 | + self.assertEqual(h.committed_at, datetime(2011, 11, 9, 8, 50, 53)) |
| 94 | + self.assertEqual(h.url, |
| 95 | + u'https://api.github.com/gists/791920/' |
| 96 | + u'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef') |
| 97 | + self.assertEqual(h.user.__dict__, gist.user.__dict__) |
| 98 | + self.assertEqual(h.version, u'deadbeefdeadbeefdeadbeefdeadbeefdeadbeef') |
| 99 | + |
| 100 | + |
| 101 | +if __name__ == '__main__': |
| 102 | + unittest.main() |
0 commit comments