def test_github_does_not_have_new_version(self):
        """ If the github query returns a SHA key that is equal to the local
        master HEAD key, check shall indicate that there is no new version
        available. """

        mock_commit = self.mox.CreateMockAnything()
        mock_commit.hash = self.__mock_local_head_hash

        self.mox.StubOutWithMock(git.LocalRepository, "getHead")
        self.mox.StubOutWithMock(BleedingEdgeUpdater, "is_repo")
        self.mox.StubOutWithMock(BleedingEdgeUpdater, "has_git")
        self.mox.StubOutWithMock(BleedingEdgeUpdater, "get_origin_head_sha")

        BleedingEdgeUpdater.is_repo().AndReturn(True)
        BleedingEdgeUpdater.has_git().AndReturn(True)
        git.LocalRepository.getHead().AndReturn(mock_commit)

        BleedingEdgeUpdater.get_origin_head_sha().AndReturn(
            self.__mock_local_head_hash)

        self.mox.ReplayAll()

        updater = BleedingEdgeUpdater(self.__repo, self.__remote_url)

        self.assertTrue(not updater.check())
    def test_getting_origin_head_hash_no_response(self):
        """ If urlopen returns None, None should be returned. """

        self.mox.StubOutWithMock(urllib2, "urlopen")
        self.mox.StubOutWithMock(json, "loads")

        urllib2.urlopen(self.__remote_url).AndReturn(None)

        self.mox.ReplayAll()

        bleeding_updater = BleedingEdgeUpdater(self.__repo, self.__remote_url)

        self.assertEquals(None, bleeding_updater.get_origin_head_sha())
    def test_getting_origin_head_hash_urlopen_exception(self):
        """ If urlopen raises an exception (timeout, invalid url, etc.), None
        should be returned. """

        self.mox.StubOutWithMock(urllib2, "urlopen")
        self.mox.StubOutWithMock(json, "loads")

        urllib2.urlopen(self.__remote_url).AndRaise(urllib2.URLError("na-ah"))

        self.mox.ReplayAll()

        bleeding_updater = BleedingEdgeUpdater(self.__repo, self.__remote_url)

        self.assertEquals(None, bleeding_updater.get_origin_head_sha())
    def test_getting_origin_head_hash_no_html(self):
        """ If we fail to read the urlopen query, None shall be returned. """

        mock_response = self.mox.CreateMockAnything()

        self.mox.StubOutWithMock(urllib2, "urlopen")

        urllib2.urlopen(self.__remote_url).AndReturn(mock_response)

        mock_response.read().AndReturn(None)

        self.mox.ReplayAll()

        bleeding_updater = BleedingEdgeUpdater(self.__repo, self.__remote_url)

        self.assertEquals(None, bleeding_updater.get_origin_head_sha())
    def test_getting_origin_head_hash_no_json(self):
        """ In the case of the read HTML not containing proper json, None shall
        be returned. """

        mock_response = self.mox.CreateMockAnything()
        mock_html = "no json for you"

        self.mox.StubOutWithMock(urllib2, "urlopen")

        urllib2.urlopen(self.__remote_url).AndReturn(mock_response)

        mock_response.read().AndReturn(mock_html)

        self.mox.ReplayAll()

        bleeding_updater = BleedingEdgeUpdater(self.__repo, self.__remote_url)

        self.assertEquals(None, bleeding_updater.get_origin_head_sha())
    def test_getting_origin_head_hash_no_sha_in_json(self):
        """ If the json response from github does not contain a SHA key, None
        shall be returned. """

        mock_response = self.mox.CreateMockAnything()
        mock_html = self.mox.CreateMockAnything()

        mock_json_dict = {'foo': 'bar'}

        self.mox.StubOutWithMock(urllib2, "urlopen")
        self.mox.StubOutWithMock(json, "loads")

        urllib2.urlopen(self.__remote_url).AndReturn(mock_response)
        mock_response.read().AndReturn(mock_html)

        json.loads(mock_html).AndReturn(mock_json_dict)

        self.mox.ReplayAll()

        bleeding_updater = BleedingEdgeUpdater(self.__repo, self.__remote_url)

        self.assertEquals(None, bleeding_updater.get_origin_head_sha())
    def test_getting_origin_head_hash(self):
        """ Sunshine case when querying github for origin/master HEAD SHA. """

        mock_response = self.mox.CreateMockAnything()
        mock_html = self.mox.CreateMockAnything()
        mock_json_dict = self.mox.CreateMockAnything()

        self.mox.StubOutWithMock(urllib2, "urlopen")
        self.mox.StubOutWithMock(json, "loads")

        urllib2.urlopen(self.__remote_url).AndReturn(mock_response)
        mock_response.read().AndReturn(mock_html)

        json.loads(mock_html).AndReturn(mock_json_dict)

        mock_json_dict["sha"].AndReturn(self.__mock_origin_head_sha)

        self.mox.ReplayAll()

        bleeding_updater = BleedingEdgeUpdater(self.__repo, self.__remote_url)

        self.assertEquals(self.__mock_origin_head_sha,
                          bleeding_updater.get_origin_head_sha())