<s> import base64 <EOL> import json <EOL> import os <EOL> import tempfile <EOL> import pytest <EOL> import responses <EOL> import six <EOL> from sparkpost import SparkPost <EOL> from sparkpost import Transmissions <EOL> from sparkpost . exceptions import SparkPostAPIException <EOL> def test_translate_keys_with_list ( ) : <EOL> t = Transmissions ( '<STR_LIT>' , '<STR_LIT:key>' ) <EOL> results = t . _translate_keys ( recipient_list = '<STR_LIT:test>' ) <EOL> assert results [ '<STR_LIT>' ] == '<STR_LIT>' <EOL> assert results [ '<STR_LIT:content>' ] [ '<STR_LIT>' ] is False <EOL> assert results [ '<STR_LIT>' ] == { '<STR_LIT>' : '<STR_LIT:test>' } <EOL> def test_translate_keys_with_recips ( ) : <EOL> t = Transmissions ( '<STR_LIT>' , '<STR_LIT:key>' ) <EOL> results = t . _translate_keys ( recipients = [ '<STR_LIT:test>' , <EOL> { '<STR_LIT:key>' : '<STR_LIT:value>' } , '<STR_LIT>' ] ) <EOL> assert results [ '<STR_LIT>' ] == [ { '<STR_LIT:address>' : { '<STR_LIT:email>' : '<STR_LIT:test>' } } , <EOL> { '<STR_LIT:key>' : '<STR_LIT:value>' } , <EOL> { '<STR_LIT:address>' : { '<STR_LIT:email>' : '<STR_LIT>' } } ] <EOL> def test_translate_keys_with_unicode_recips ( ) : <EOL> t = Transmissions ( '<STR_LIT>' , '<STR_LIT:key>' ) <EOL> results = t . _translate_keys ( recipients = [ u'<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> assert results [ '<STR_LIT>' ] == [ <EOL> { '<STR_LIT:address>' : { '<STR_LIT:email>' : '<STR_LIT>' } } , <EOL> { '<STR_LIT:address>' : { '<STR_LIT:email>' : '<STR_LIT>' } } <EOL> ] <EOL> def test_translate_keys_for_email_parsing ( ) : <EOL> t = Transmissions ( '<STR_LIT>' , '<STR_LIT:key>' ) <EOL> results = t . _translate_keys ( recipients = [ '<STR_LIT>' , <EOL> '<STR_LIT>' ] ) <EOL> assert results [ '<STR_LIT>' ] == [ <EOL> { '<STR_LIT:address>' : { '<STR_LIT:email>' : '<STR_LIT>' } } , <EOL> { '<STR_LIT:address>' : { '<STR_LIT:name>' : '<STR_LIT>' , '<STR_LIT:email>' : '<STR_LIT>' } } <EOL> ] <EOL> def test_translate_keys_for_from_email ( ) : <EOL> t = Transmissions ( '<STR_LIT>' , '<STR_LIT:key>' ) <EOL> results = t . _translate_keys ( from_email = '<STR_LIT>' ) <EOL> assert results [ '<STR_LIT:content>' ] [ '<STR_LIT>' ] == { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:email>' : '<STR_LIT>' <EOL> } <EOL> def test_translate_keys_with_cc ( ) : <EOL> t = Transmissions ( '<STR_LIT>' , '<STR_LIT:key>' ) <EOL> results = t . _translate_keys ( recipients = [ '<STR_LIT>' ] , <EOL> cc = [ '<STR_LIT>' ] ) <EOL> assert results [ '<STR_LIT>' ] == [ <EOL> { '<STR_LIT:address>' : { '<STR_LIT:email>' : '<STR_LIT>' } } , <EOL> { '<STR_LIT:address>' : { '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } , <EOL> ] <EOL> assert results [ '<STR_LIT:content>' ] [ '<STR_LIT>' ] == { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def test_translate_keys_with_multiple_cc ( ) : <EOL> t = Transmissions ( '<STR_LIT>' , '<STR_LIT:key>' ) <EOL> results = t . _translate_keys ( recipients = [ '<STR_LIT>' ] , <EOL> cc = [ '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> assert results [ '<STR_LIT>' ] == [ <EOL> { '<STR_LIT:address>' : { '<STR_LIT:email>' : '<STR_LIT>' } } , <EOL> { '<STR_LIT:address>' : { '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } , <EOL> { '<STR_LIT:address>' : { '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } , <EOL> ] <EOL> assert results [ '<STR_LIT:content>' ] [ '<STR_LIT>' ] == { <EOL> '<STR_LIT>' : '<STR_LIT>' <EOL> } <EOL> def test_translate_keys_with_bcc ( ) : <EOL> t = Transmissions ( '<STR_LIT>' , '<STR_LIT:key>' ) <EOL> results = t . _translate_keys ( recipients = [ '<STR_LIT>' ] , <EOL> bcc = [ '<STR_LIT>' ] ) <EOL> assert results [ '<STR_LIT>' ] == [ <EOL> { '<STR_LIT:address>' : { '<STR_LIT:email>' : '<STR_LIT>' } } , <EOL> { '<STR_LIT:address>' : { '<STR_LIT:email>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' } } , <EOL> ] <EOL> def test_translate_keys_with_inline_css ( ) : <EOL> t = Transmissions ( '<STR_LIT>' , '<STR_LIT:key>' ) <EOL> results = t . _translate_keys ( inline_css = True ) <EOL> assert results [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) is True <EOL> @ responses . activate <EOL> def test_success_send ( ) : <EOL> responses . add ( <EOL> responses . POST , <EOL> '<STR_LIT>' , <EOL> status = <NUM_LIT:200> , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> body = '<STR_LIT>' <EOL> ) <EOL> sp = SparkPost ( '<STR_LIT>' ) <EOL> results = sp . transmission . send ( ) <EOL> assert results == '<STR_LIT>' <EOL> @ responses . activate <EOL> def test_success_send_with_attachments ( ) : <EOL> try : <EOL> test_content = six . u ( "<STR_LIT>" ) <EOL> ( _ , temp_file_path ) = tempfile . mkstemp ( ) <EOL> with open ( temp_file_path , "<STR_LIT:w>" ) as temp_file : <EOL> temp_file . write ( test_content ) <EOL> responses . add ( <EOL> responses . POST , <EOL> '<STR_LIT>' , <EOL> status = <NUM_LIT:200> , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> body = '<STR_LIT>' <EOL> ) <EOL> sp = SparkPost ( '<STR_LIT>' ) <EOL> attachment = { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT:filename>" : temp_file_path <EOL> } <EOL> results = sp . transmission . send ( attachments = [ attachment ] ) <EOL> request_params = json . loads ( responses . calls [ <NUM_LIT:0> ] . request . body ) <EOL> content = base64 . b64decode ( <EOL> request_params [ "<STR_LIT:content>" ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT:data>" ] ) <EOL> assert test_content == content . decode ( "<STR_LIT:ascii>" ) <EOL> assert results == '<STR_LIT>' <EOL> attachment = { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT:data>" : base64 . b64encode ( <EOL> test_content . encode ( "<STR_LIT:ascii>" ) ) . decode ( "<STR_LIT:ascii>" ) <EOL> } <EOL> results = sp . transmission . send ( attachments = [ attachment ] ) <EOL> request_params = json . loads ( responses . calls [ <NUM_LIT:1> ] . request . body ) <EOL> content = base64 . b64decode ( <EOL> request_params [ "<STR_LIT:content>" ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT:data>" ] ) <EOL> assert test_content == content . decode ( "<STR_LIT:ascii>" ) <EOL> assert results == '<STR_LIT>' <EOL> finally : <EOL> os . unlink ( temp_file_path ) <EOL> @ responses . activate <EOL> def test_success_send_with_inline_images ( ) : <EOL> current_dir = os . path . abspath ( os . path . dirname ( __file__ ) ) <EOL> image_path = os . path . join ( current_dir , '<STR_LIT>' , '<STR_LIT>' ) <EOL> with open ( image_path , "<STR_LIT:rb>" ) as image : <EOL> encoded_image = base64 . b64encode ( image . read ( ) ) . decode ( "<STR_LIT:ascii>" ) <EOL> responses . add ( <EOL> responses . POST , <EOL> '<STR_LIT>' , <EOL> status = <NUM_LIT:200> , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> body = '<STR_LIT>' <EOL> ) <EOL> sp = SparkPost ( '<STR_LIT>' ) <EOL> image_data = { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT:filename>" : image_path <EOL> } <EOL> results = sp . transmission . send ( inline_images = [ image_data ] ) <EOL> request_params = json . loads ( responses . calls [ <NUM_LIT:0> ] . request . body ) <EOL> content = request_params [ "<STR_LIT:content>" ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT:data>" ] <EOL> assert encoded_image == content <EOL> assert results == '<STR_LIT>' <EOL> image_data = { <EOL> "<STR_LIT:name>" : "<STR_LIT>" , <EOL> "<STR_LIT:type>" : "<STR_LIT>" , <EOL> "<STR_LIT:data>" : encoded_image <EOL> } <EOL> results = sp . transmission . send ( inline_images = [ image_data ] ) <EOL> request_params = json . loads ( responses . calls [ <NUM_LIT:1> ] . request . body ) <EOL> content = request_params [ "<STR_LIT:content>" ] [ "<STR_LIT>" ] [ <NUM_LIT:0> ] [ "<STR_LIT:data>" ] <EOL> assert content == encoded_image <EOL> assert results == '<STR_LIT>' <EOL> @ responses . activate <EOL> def test_fail_send ( ) : <EOL> responses . add ( <EOL> responses . POST , <EOL> '<STR_LIT>' , <EOL> status = <NUM_LIT> , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> body = """<STR_LIT>""" <EOL> ) <EOL> with pytest . raises ( SparkPostAPIException ) : <EOL> sp = SparkPost ( '<STR_LIT>' ) <EOL> sp . transmission . send ( ) <EOL> @ responses . activate <EOL> def test_success_get ( ) : <EOL> responses . add ( <EOL> responses . GET , <EOL> '<STR_LIT>' , <EOL> status = <NUM_LIT:200> , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> body = '<STR_LIT>' <EOL> ) <EOL> sp = SparkPost ( '<STR_LIT>' ) <EOL> results = sp . transmission . get ( '<STR_LIT>' ) <EOL> assert results == { } <EOL> @ responses . activate <EOL> def test_fail_get ( ) : <EOL> responses . add ( <EOL> responses . GET , <EOL> '<STR_LIT>' , <EOL> status = <NUM_LIT> , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> body = """<STR_LIT>""" <EOL> ) <EOL> with pytest . raises ( SparkPostAPIException ) : <EOL> sp = SparkPost ( '<STR_LIT>' ) <EOL> sp . transmission . get ( '<STR_LIT>' ) <EOL> @ responses . activate <EOL> def test_success_list ( ) : <EOL> responses . add ( <EOL> responses . GET , <EOL> '<STR_LIT>' , <EOL> status = <NUM_LIT:200> , <EOL> content_type = '<STR_LIT:application/json>' , <EOL> body = '<STR_LIT>' <EOL> ) <EOL> sp = SparkPost ( '<STR_LIT>' ) <EOL> response = sp . transmission . list ( ) <EOL> assert response == [ ] </s>
