<s> from __future__ import absolute_import <EOL> import mock <EOL> from django . core . urlresolvers import reverse <EOL> from exam import fixture <EOL> from sentry . models import ProjectKey <EOL> from sentry . testutils import TestCase <EOL> from sentry . utils import json <EOL> class CspReportViewTest ( TestCase ) : <EOL> @ fixture <EOL> def path ( self ) : <EOL> path = reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . project . id } ) <EOL> return path + '<STR_LIT>' % self . projectkey . public_key <EOL> def test_get_response ( self ) : <EOL> resp = self . client . get ( self . path ) <EOL> assert resp . status_code == <NUM_LIT> , resp . content <EOL> def test_invalid_content_type ( self ) : <EOL> resp = self . client . post ( self . path , content_type = '<STR_LIT>' ) <EOL> assert resp . status_code == <NUM_LIT> , resp . content <EOL> def test_missing_csp_report ( self ) : <EOL> resp = self . client . post ( self . path , <EOL> content_type = '<STR_LIT>' , <EOL> data = '<STR_LIT>' , <EOL> HTTP_USER_AGENT = '<STR_LIT>' , <EOL> ) <EOL> assert resp . status_code == <NUM_LIT> , resp . content <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_bad_origin ( self , get_origins ) : <EOL> get_origins . return_value = [ '<STR_LIT>' ] <EOL> resp = self . client . post ( self . path , <EOL> content_type = '<STR_LIT>' , <EOL> data = '<STR_LIT>' , <EOL> HTTP_USER_AGENT = '<STR_LIT>' , <EOL> ) <EOL> assert resp . status_code == <NUM_LIT> , resp . content <EOL> get_origins . return_value = [ '<STR_LIT:*>' ] <EOL> resp = self . client . post ( self . path , <EOL> content_type = '<STR_LIT>' , <EOL> data = '<STR_LIT>' , <EOL> HTTP_USER_AGENT = '<STR_LIT>' , <EOL> ) <EOL> assert resp . status_code == <NUM_LIT> , resp . content <EOL> @ mock . patch ( '<STR_LIT>' , mock . Mock ( return_value = True ) ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_post_success ( self , process ) : <EOL> process . return_value = '<STR_LIT>' <EOL> resp = self . _postCspWithHeader ( { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } ) <EOL> assert resp . status_code == <NUM_LIT> , resp . content <EOL> class StoreViewTest ( TestCase ) : <EOL> @ fixture <EOL> def path ( self ) : <EOL> return reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . project . id } ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_options_response ( self , parse_header ) : <EOL> project = self . create_project ( ) <EOL> pk = ProjectKey . objects . get_or_create ( project = project ) [ <NUM_LIT:0> ] <EOL> parse_header . return_value = { <EOL> '<STR_LIT>' : project . id , <EOL> '<STR_LIT>' : pk . public_key , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } <EOL> resp = self . client . options ( self . path ) <EOL> assert resp . status_code == <NUM_LIT:200> , resp . content <EOL> self . assertIn ( '<STR_LIT>' , resp ) <EOL> self . assertEquals ( resp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> self . assertIn ( '<STR_LIT>' , resp ) <EOL> self . assertEquals ( resp [ '<STR_LIT>' ] , '<STR_LIT:0>' ) <EOL> @ mock . patch ( '<STR_LIT>' , mock . Mock ( return_value = False ) ) <EOL> def test_options_response_with_invalid_origin ( self ) : <EOL> resp = self . client . options ( self . path , HTTP_ORIGIN = '<STR_LIT>' ) <EOL> assert resp . status_code == <NUM_LIT> , resp . content <EOL> self . assertIn ( '<STR_LIT>' , resp ) <EOL> self . assertEquals ( resp [ '<STR_LIT>' ] , '<STR_LIT:*>' ) <EOL> self . assertIn ( '<STR_LIT>' , resp ) <EOL> assert resp [ '<STR_LIT>' ] == "<STR_LIT>" <EOL> assert json . loads ( resp . content ) [ '<STR_LIT:error>' ] == resp [ '<STR_LIT>' ] <EOL> @ mock . patch ( '<STR_LIT>' , mock . Mock ( return_value = False ) ) <EOL> def test_options_response_with_invalid_referrer ( self ) : <EOL> resp = self . client . options ( self . path , HTTP_REFERER = '<STR_LIT>' ) <EOL> assert resp . status_code == <NUM_LIT> , resp . content <EOL> self . assertIn ( '<STR_LIT>' , resp ) <EOL> self . assertEquals ( resp [ '<STR_LIT>' ] , '<STR_LIT:*>' ) <EOL> self . assertIn ( '<STR_LIT>' , resp ) <EOL> assert resp [ '<STR_LIT>' ] == "<STR_LIT>" <EOL> assert json . loads ( resp . content ) [ '<STR_LIT:error>' ] == resp [ '<STR_LIT>' ] <EOL> @ mock . patch ( '<STR_LIT>' , mock . Mock ( return_value = True ) ) <EOL> def test_options_response_with_valid_origin ( self ) : <EOL> resp = self . client . options ( self . path , HTTP_ORIGIN = '<STR_LIT>' ) <EOL> assert resp . status_code == <NUM_LIT:200> , resp . content <EOL> self . assertIn ( '<STR_LIT>' , resp ) <EOL> self . assertEquals ( resp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , mock . Mock ( return_value = True ) ) <EOL> def test_options_response_with_valid_referrer ( self ) : <EOL> resp = self . client . options ( self . path , HTTP_REFERER = '<STR_LIT>' ) <EOL> assert resp . status_code == <NUM_LIT:200> , resp . content <EOL> self . assertIn ( '<STR_LIT>' , resp ) <EOL> self . assertEquals ( resp [ '<STR_LIT>' ] , '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' , mock . Mock ( return_value = False ) ) <EOL> def test_request_with_backlisted_ip ( self ) : <EOL> resp = self . _postWithHeader ( { } ) <EOL> assert resp . status_code == <NUM_LIT> , resp . content <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_scrubs_ip_address ( self , mock_insert_data_to_database ) : <EOL> self . project . update_option ( '<STR_LIT>' , True ) <EOL> body = { <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT:127.0.0.1>" } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT:GET>" , <EOL> "<STR_LIT:url>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT:127.0.0.1>" } <EOL> } , <EOL> } <EOL> resp = self . _postWithHeader ( body ) <EOL> assert resp . status_code == <NUM_LIT:200> , resp . content <EOL> call_data = mock_insert_data_to_database . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> assert not call_data [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> assert not call_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_scrubs_org_ip_address_override ( self , mock_insert_data_to_database ) : <EOL> self . organization . update_option ( '<STR_LIT>' , True ) <EOL> self . project . update_option ( '<STR_LIT>' , False ) <EOL> body = { <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT:127.0.0.1>" } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT:GET>" , <EOL> "<STR_LIT:url>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT:127.0.0.1>" } <EOL> } , <EOL> } <EOL> resp = self . _postWithHeader ( body ) <EOL> assert resp . status_code == <NUM_LIT:200> , resp . content <EOL> call_data = mock_insert_data_to_database . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> assert not call_data [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> assert not call_data [ '<STR_LIT>' ] [ '<STR_LIT>' ] . get ( '<STR_LIT>' ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_scrub_data_off ( self , mock_insert_data_to_database ) : <EOL> self . project . update_option ( '<STR_LIT>' , False ) <EOL> self . project . update_option ( '<STR_LIT>' , False ) <EOL> body = { <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT:127.0.0.1>" } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT:GET>" , <EOL> "<STR_LIT:url>" : "<STR_LIT>" , <EOL> "<STR_LIT:data>" : "<STR_LIT>" <EOL> } , <EOL> } <EOL> resp = self . _postWithHeader ( body ) <EOL> assert resp . status_code == <NUM_LIT:200> , resp . content <EOL> call_data = mock_insert_data_to_database . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> assert call_data [ '<STR_LIT>' ] [ '<STR_LIT:data>' ] == '<STR_LIT>' <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_scrub_data_on ( self , mock_insert_data_to_database ) : <EOL> self . project . update_option ( '<STR_LIT>' , True ) <EOL> self . project . update_option ( '<STR_LIT>' , False ) <EOL> body = { <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT:127.0.0.1>" } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT:GET>" , <EOL> "<STR_LIT:url>" : "<STR_LIT>" , <EOL> "<STR_LIT:data>" : "<STR_LIT>" <EOL> } , <EOL> } <EOL> resp = self . _postWithHeader ( body ) <EOL> assert resp . status_code == <NUM_LIT:200> , resp . content <EOL> call_data = mock_insert_data_to_database . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> assert call_data [ '<STR_LIT>' ] [ '<STR_LIT:data>' ] == '<STR_LIT>' <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_scrub_data_defaults ( self , mock_insert_data_to_database ) : <EOL> self . project . update_option ( '<STR_LIT>' , True ) <EOL> self . project . update_option ( '<STR_LIT>' , True ) <EOL> body = { <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT:127.0.0.1>" } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT:GET>" , <EOL> "<STR_LIT:url>" : "<STR_LIT>" , <EOL> "<STR_LIT:data>" : "<STR_LIT>" <EOL> } , <EOL> } <EOL> resp = self . _postWithHeader ( body ) <EOL> assert resp . status_code == <NUM_LIT:200> , resp . content <EOL> call_data = mock_insert_data_to_database . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> assert call_data [ '<STR_LIT>' ] [ '<STR_LIT:data>' ] == '<STR_LIT>' <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_scrub_data_sensitive_fields ( self , mock_insert_data_to_database ) : <EOL> self . project . update_option ( '<STR_LIT>' , True ) <EOL> self . project . update_option ( '<STR_LIT>' , True ) <EOL> self . project . update_option ( '<STR_LIT>' , [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] ) <EOL> body = { <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT:127.0.0.1>" } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT:GET>" , <EOL> "<STR_LIT:url>" : "<STR_LIT>" , <EOL> "<STR_LIT:data>" : "<STR_LIT>" <EOL> } , <EOL> } <EOL> resp = self . _postWithHeader ( body ) <EOL> assert resp . status_code == <NUM_LIT:200> , resp . content <EOL> call_data = mock_insert_data_to_database . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> assert call_data [ '<STR_LIT>' ] [ '<STR_LIT:data>' ] == '<STR_LIT>' <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_scrub_data_org_override ( self , mock_insert_data_to_database ) : <EOL> self . organization . update_option ( '<STR_LIT>' , True ) <EOL> self . project . update_option ( '<STR_LIT>' , False ) <EOL> self . organization . update_option ( '<STR_LIT>' , True ) <EOL> self . project . update_option ( '<STR_LIT>' , False ) <EOL> body = { <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT:127.0.0.1>" } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT:GET>" , <EOL> "<STR_LIT:url>" : "<STR_LIT>" , <EOL> "<STR_LIT:data>" : "<STR_LIT>" <EOL> } , <EOL> } <EOL> resp = self . _postWithHeader ( body ) <EOL> assert resp . status_code == <NUM_LIT:200> , resp . content <EOL> call_data = mock_insert_data_to_database . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> assert call_data [ '<STR_LIT>' ] [ '<STR_LIT:data>' ] == '<STR_LIT>' <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_scrub_data_org_override_sensitive_fields ( self , mock_insert_data_to_database ) : <EOL> self . organization . update_option ( '<STR_LIT>' , True ) <EOL> self . organization . update_option ( '<STR_LIT>' , True ) <EOL> self . organization . update_option ( '<STR_LIT>' , [ '<STR_LIT>' ] ) <EOL> self . project . update_option ( '<STR_LIT>' , [ '<STR_LIT:foo>' , '<STR_LIT:bar>' ] ) <EOL> body = { <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> "<STR_LIT>" : { "<STR_LIT>" : "<STR_LIT:127.0.0.1>" } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT>" : "<STR_LIT:GET>" , <EOL> "<STR_LIT:url>" : "<STR_LIT>" , <EOL> "<STR_LIT:data>" : "<STR_LIT>" <EOL> } , <EOL> } <EOL> resp = self . _postWithHeader ( body ) <EOL> assert resp . status_code == <NUM_LIT:200> , resp . content <EOL> call_data = mock_insert_data_to_database . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> assert call_data [ '<STR_LIT>' ] [ '<STR_LIT:data>' ] == '<STR_LIT>' <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_uses_client_as_sdk ( self , mock_insert_data_to_database ) : <EOL> body = { <EOL> "<STR_LIT:message>" : "<STR_LIT>" , <EOL> } <EOL> resp = self . _postWithHeader ( body ) <EOL> assert resp . status_code == <NUM_LIT:200> , resp . content <EOL> call_data = mock_insert_data_to_database . call_args [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> assert call_data [ '<STR_LIT>' ] == { <EOL> '<STR_LIT:name>' : '<STR_LIT>' , <EOL> '<STR_LIT:version>' : '<STR_LIT>' , <EOL> } <EOL> class CrossDomainXmlTest ( TestCase ) : <EOL> @ fixture <EOL> def path ( self ) : <EOL> return reverse ( '<STR_LIT>' , kwargs = { '<STR_LIT>' : self . project . id } ) <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_output_with_global ( self , get_origins ) : <EOL> get_origins . return_value = '<STR_LIT:*>' <EOL> resp = self . client . get ( self . path ) <EOL> get_origins . assert_called_once_with ( self . project ) <EOL> assert resp . status_code == <NUM_LIT:200> , resp . content <EOL> self . assertEquals ( resp [ '<STR_LIT:Content-Type>' ] , '<STR_LIT>' ) <EOL> self . assertTemplateUsed ( resp , '<STR_LIT>' ) <EOL> assert '<STR_LIT>' in resp . content <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_output_with_whitelist ( self , get_origins ) : <EOL> get_origins . return_value = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> resp = self . client . get ( self . path ) <EOL> get_origins . assert_called_once_with ( self . project ) <EOL> self . assertEquals ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEquals ( resp [ '<STR_LIT:Content-Type>' ] , '<STR_LIT>' ) <EOL> self . assertTemplateUsed ( resp , '<STR_LIT>' ) <EOL> assert '<STR_LIT>' in resp . content <EOL> assert '<STR_LIT>' in resp . content <EOL> @ mock . patch ( '<STR_LIT>' ) <EOL> def test_output_with_no_origins ( self , get_origins ) : <EOL> get_origins . return_value = [ ] <EOL> resp = self . client . get ( self . path ) <EOL> get_origins . assert_called_once_with ( self . project ) <EOL> self . assertEquals ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEquals ( resp [ '<STR_LIT:Content-Type>' ] , '<STR_LIT>' ) <EOL> self . assertTemplateUsed ( resp , '<STR_LIT>' ) <EOL> assert '<STR_LIT>' not in resp . content <EOL> def test_output_allows_x_sentry_auth ( self ) : <EOL> resp = self . client . get ( self . path ) <EOL> self . assertEquals ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEquals ( resp [ '<STR_LIT:Content-Type>' ] , '<STR_LIT>' ) <EOL> self . assertTemplateUsed ( resp , '<STR_LIT>' ) <EOL> assert '<STR_LIT>' in resp . content <EOL> class CrossDomainXmlIndexTest ( TestCase ) : <EOL> @ fixture <EOL> def path ( self ) : <EOL> return reverse ( '<STR_LIT>' ) <EOL> def test_permits_policies ( self ) : <EOL> resp = self . client . get ( self . path ) <EOL> self . assertEquals ( resp . status_code , <NUM_LIT:200> ) <EOL> self . assertEquals ( resp [ '<STR_LIT:Content-Type>' ] , '<STR_LIT>' ) <EOL> self . assertTemplateUsed ( resp , '<STR_LIT>' ) <EOL> assert '<STR_LIT>' in resp . content <EOL> class RobotsTxtTest ( TestCase ) : <EOL> @ fixture <EOL> def path ( self ) : <EOL> return reverse ( '<STR_LIT>' ) <EOL> def test_robots ( self ) : <EOL> resp = self . client . get ( self . path ) <EOL> assert resp . status_code == <NUM_LIT:200> <EOL> assert resp [ '<STR_LIT:Content-Type>' ] == '<STR_LIT>' </s>
