def test_init_params(self):
        """
        Test that the passed in params set the expected object attributes.
        """

        expected_secret = (
            '-----BEGIN PRIVATE KEY-----\n'
            'MIGEAgEAMBAGByqGSM49AgEGBSuBBAAKBG0wawIBAQQg2yIXGwM2PGlbigymjZvd\n'
            'ouICGOKxRRiT3I4rOxaCTXuhRANCAASYKQIMh0jBbQ5MmzwldwnNTQPtbeOfFrf5\n'
            'NzrljP9ZBHJhjfe0O1wRnOzGDbpJuMQrZFppIEnaXZ5/0Q+wpPIt\n'
            '-----END PRIVATE KEY-----\n')
        connection = jwt_apns_client.APNSConnection(
            algorithm='HS256',
            team_id='TEAMID',
            apns_key_id='asdf1234',
            apns_key_path=self.KEY_FILE_PATH,
            api_version=2,  # not a value we would really want to pass
            environment=jwt_apns_client.APNSEnvironments.PROD,
            api_host='api.example.org',
            api_port=442)
        self.assertEqual('HS256', connection.algorithm)
        self.assertEqual('TEAMID', connection.team_id)
        self.assertEqual('asdf1234', connection.apns_key_id)
        self.assertEqual(self.KEY_FILE_PATH, connection.apns_key_path)
        self.assertEqual(2, connection.api_version)
        self.assertEqual('prod', connection.environment)
        self.assertEqual('api.example.org', connection.api_host)
        self.assertEqual(442, connection.api_port)
        self.assertEqual(expected_secret, connection.secret)
        self.assertEqual(None, connection._conn)
    def test_get_token_headers_returns_dict_with_correct_keys(self):
        connection = jwt_apns_client.APNSConnection(
            team_id='TEAMID',
            apns_key_id='KEYID',
            apns_key_path=self.KEY_FILE_PATH)

        self.assertEqual({
            'alg': 'ES256',
            'kid': 'KEYID'
        }, connection.get_token_headers())
 def test_send_notification_good(self, HTTPConnectionMock):
     HTTPConnectionMock.return_value.get_response.return_value = make_http_response_mock(
     )
     connection = jwt_apns_client.APNSConnection(
         team_id='TEAMID',
         apns_key_id='KEYID',
         apns_key_path=self.KEY_FILE_PATH)
     response = connection.send_notification(
         device_registration_id='asdf12345', alert='Testing')
     self.assertTrue(
         isinstance(response, jwt_apns_client.NotificationResponse))
     self.assertEqual(1, HTTPConnectionMock.call_count)
    def test_get_secret(self):
        expected_secret = (
            '-----BEGIN PRIVATE KEY-----\n'
            'MIGEAgEAMBAGByqGSM49AgEGBSuBBAAKBG0wawIBAQQg2yIXGwM2PGlbigymjZvd\n'
            'ouICGOKxRRiT3I4rOxaCTXuhRANCAASYKQIMh0jBbQ5MmzwldwnNTQPtbeOfFrf5\n'
            'NzrljP9ZBHJhjfe0O1wRnOzGDbpJuMQrZFppIEnaXZ5/0Q+wpPIt\n'
            '-----END PRIVATE KEY-----\n')
        connection = jwt_apns_client.APNSConnection(
            team_id='TEAMID',
            apns_key_id='KEYID',
            apns_key_path=self.KEY_FILE_PATH)

        self.assertEqual(expected_secret, connection.get_secret())
 def test_init_params_default(self):
     """
     Test the default init params
     """
     connection = jwt_apns_client.APNSConnection()
     self.assertEqual('ES256', connection.algorithm)
     self.assertEqual(None, connection.team_id)
     self.assertEqual(None, connection.apns_key_id)
     self.assertEqual(None, connection.apns_key_path)
     self.assertEqual(3, connection.api_version)
     self.assertEqual('dev', connection.environment)
     self.assertEqual('api.development.push.apple.com', connection.api_host)
     self.assertEqual(443, connection.api_port)
     self.assertEqual('', connection.secret)
     self.assertEqual(None, connection._conn)
 def test_send_notification_with_error(self, HTTPConnectionMock):
     response_mock = make_http_response_mock(
         status=400, reason=APNSReasons.MISSING_DEVICE_TOKEN)
     HTTPConnectionMock.return_value.get_response.return_value = response_mock
     connection = jwt_apns_client.APNSConnection(
         team_id='TEAMID',
         apns_key_id='KEYID',
         apns_key_path=self.KEY_FILE_PATH)
     http2conn = connection.connection
     response = connection.send_notification(
         device_registration_id='asdf12345', alert='Testing')
     self.assertTrue(
         isinstance(response, jwt_apns_client.NotificationResponse))
     self.assertEqual(400, response.status)
     self.assertEqual(APNSReasons.MISSING_DEVICE_TOKEN, response.reason)
     self.assertIsNotNone(
         connection._conn)  # old connection was not cleared.
    def test_send_notification_with_idle_timeout(self, HTTPConnectionMock):
        """
        Test that when an idle timeout error is received the connection is cleared/reset
        """
        response_mock = make_http_response_mock(
            status=400, reason=APNSReasons.IDLE_TIMEOUT)
        HTTPConnectionMock.return_value.get_response.return_value = response_mock

        connection = jwt_apns_client.APNSConnection(
            team_id='TEAMID',
            apns_key_id='KEYID',
            apns_key_path=self.KEY_FILE_PATH)
        response = connection.send_notification(
            device_registration_id='asdf12345', alert='Testing')
        self.assertTrue(
            isinstance(response, jwt_apns_client.NotificationResponse))
        self.assertEqual(400, response.status)
        self.assertEqual(APNSReasons.IDLE_TIMEOUT, response.reason)
        self.assertIsNone(connection._conn)  # old connection was cleared.
    def test_make_provider_token_calls_jwt_encode_with_correct_args(self):
        """
        Test that APNSConnection.make_provider_token() calls jwt.encode() with the correct
        arguments.  jwt.encode() returns different results each time even with the same data passed in
        so we cannot just test for the expected return value.
        """
        issued_at = time.time()
        connection = jwt_apns_client.APNSConnection(
            team_id='TEAMID',
            apns_key_id='KEYID',
            apns_key_path=self.KEY_FILE_PATH)

        with mock.patch('jwt_apns_client.utils.jwt.encode') as mock_encode:
            connection.make_provider_token(issued_at=issued_at)
            mock_encode.assert_called_with(
                {
                    'iss': connection.team_id,
                    'iat': issued_at
                },
                connection.secret,
                algorithm=connection.algorithm,
                headers=connection.get_token_headers())
 def test_make_provider_token_encodes_correctly(self):
     """
     Run the token returned by make_provider_token back through jwt.decode() and verify that the expected
     payload is there.
     """
     issued_at = time.time()
     connection = jwt_apns_client.APNSConnection(
         team_id='TEAMID',
         apns_key_id='KEYID',
         apns_key_path=self.KEY_FILE_PATH)
     token = connection.make_provider_token(issued_at=issued_at)
     options = {
         'verify_signature': False,
         'verify_exp': False,
         'verify_nbf': False,
         'verify_iat': False,
         'verify_aud': False
     }
     decoded = jwt.decode(token,
                          connection.secret,
                          algorithm=connection.algorithm,
                          headers=connection.get_token_headers(),
                          options=options)
     self.assertEqual(decoded, {'iat': issued_at, 'iss': 'TEAMID'})