def setUp(self):
     self.app = Addon(key=u"∂ƒ©˙∆˚").save()
     self.install = Install(app=self.app, group_id=0)
     self.assertIsNone(self.install.installed_at)
     self.install.save()
     self.assertIsNotNone(self.install.installed_at)
 def test_http_auth(self):
     obj = Install(oauth_id="foo", oauth_secret="bar")
     auth = obj.http_auth()
     self.assertEqual(auth.username, obj.oauth_id)
     self.assertEqual(auth.password, obj.oauth_secret)
class InstallTests(TransactionTestCase):

    """Test suite for Install model attrs and methods."""

    fixtures = ['scopes.json']

    def setUp(self):
        self.app = Addon(key=u"∂ƒ©˙∆˚").save()
        self.install = Install(app=self.app, group_id=0)
        self.assertIsNone(self.install.installed_at)
        self.install.save()
        self.assertIsNotNone(self.install.installed_at)

    def test_strings(self):
        obj = self.install
        # confirm they don't blow up.
        self.assertIsNotNone(unicode(obj))
        self.assertIsNotNone(str(obj))
        self.assertIsNotNone(repr(obj))

    def test_save(self):
        obj = self.install.save()
        # we're just confirming that the save method
        # returns self
        self.assertIsNotNone(obj)

    def test_http_auth(self):
        obj = Install(oauth_id="foo", oauth_secret="bar")
        auth = obj.http_auth()
        self.assertEqual(auth.username, obj.oauth_id)
        self.assertEqual(auth.password, obj.oauth_secret)

    def test_parse_json(self):
        data = {
            "capabilitiesUrl": "https://api.hipchat.com/v2/capabilities",
            "oauthId": "abc",
            "oauthSecret": "xyz",
            "groupId": 123,
            # "roomId": "1234"
        }
        obj = self.install.parse_json(data)
        self.assertEqual(obj.oauth_id, 'abc')
        self.assertEqual(obj.oauth_secret, 'xyz')
        self.assertEqual(obj.group_id, 123)
        self.assertEqual(obj.room_id, None)
        data['roomId'] = '123'
        obj = self.install.parse_json(data)
        self.assertEqual(obj.room_id, 123)

    def test_token_request_data(self):
        obj = self.install
        with mock.patch('hipchat.models.Addon.scopes_as_string', lambda x: 'foo'):
            self.assertEqual(
                obj.token_request_data(),
                {'scope': 'foo', 'grant_type': 'client_credentials'}
            )

    def test_get_access_token(self):
        token_data = {
            'access_token': '52363462337245724',
            'expires_in': 3599,
            'group_id': 123,
            'group_name': 'Example Company',
            'scope': 'send_notification',
            'token_type': 'bearer'
        }
        self.assertFalse(AccessToken.objects.exists())
        with mock.patch('hipchat.models.request_access_token', lambda x: token_data):
            token = self.install.get_access_token()
            self.assertEqual(token, AccessToken.objects.get())
            self.assertEqual(token.app, self.app)
            self.assertEqual(token.install, self.install)
            self.assertEqual(token.group_id, 123)
            self.assertEqual(token.group_name, 'Example Company')
            self.assertEqual(token.scope, 'send_notification')
            self.assertIsNotNone(token.created_at)