<commit_before>use Mojolicious::Lite;
use Mojo::Loader 'data_section';
use Time::Piece;
use Test::More;
use Mojo::WebService::Twitter;
use Mojo::WebService::Twitter::Util 'twitter_authorize_url';
use Mojo::Util 'b64_encode';

use constant TEST_API_KEY => 'testapikey';
use constant TEST_API_SECRET => 'testapisecret';
use constant TEST_OAUTH_REQUEST_TOKEN => 'testrequesttoken';
use constant TEST_OAUTH_REQUEST_SECRET => 'testrequestsecret';
use constant TEST_OAUTH_VERIFIER => 'thisisaverifier';
use constant TEST_OAUTH_TOKEN => '<PASSWORD>';
use constant TEST_OAUTH_SECRET => 'testaccesssecret';

app->types->type(form => 'application/x-www-form-urlencoded');

post '/oauth/request_token' => { format => 'form', text => 'oauth_callback_confirmed=true&oauth_token='.TEST_OAUTH_REQUEST_TOKEN.'&oauth_token_secret='.TEST_OAUTH_REQUEST_SECRET };
post '/oauth/access_token' => sub {
	my $c = shift;
	my $oauth_header = $c->req->headers->authorization;
	my ($api_key) = $oauth_header =~ m/oauth_consumer_key="([^"]+)"/;
	is $api_key, TEST_API_KEY, 'received API key';
	my ($verifier) = $oauth_header =~ m/oauth_verifier="([^"]+)"/;
	is $verifier, TEST_OAUTH_VERIFIER, 'received OAuth verifier';
	$c->render(format => 'form', text => 'oauth_token='.TEST_OAUTH_TOKEN.'&oauth_token_secret='.TEST_OAUTH_SECRET);
};
group {
	under '/api' => sub {
		my $c = shift;
		my $oauth_header = $c->req->headers->authorization;
		my ($api_key) = $oauth_header =~ m/oauth_consumer_key="([^"]+)"/;
		is $api_key, TEST_API_KEY, 'received API key';
		my ($access_token) = $oauth_header =~ m/oauth_token="([^"]+)"/;
		is $access_token, TEST_OAUTH_TOKEN, 'received access token';
		return 1;
	};
	get '/account/verify_credentials.json' => { format => 'json', text => data_section('main', 'verify_credentials') };
	post '/statuses/update.json' => { format => 'json', text => data_section('main', 'post_tweet') };
};

my $api_key = $ENV{TWITTER_API_KEY};
my $api_secret = $ENV{TWITTER_API_SECRET};
my $access_token;
my $access_secret;

my $test_online = 0;
if ($ENV{AUTHOR_TESTING} and defined $api_key and defined $api_secret) {
	diag 'Running online test for Twitter';
	$test_online = 1;
} else {
	diag 'Running offline test for Twitter; set AUTHOR_TESTING and TWITTER_API_KEY/TWITTER_API_SECRET for online test';
	$Mojo::WebService::Twitter::Util::API_BASE_URL = '/api/';
	$Mojo::WebService::Twitter::Util::OAUTH_BASE_URL = '/oauth/';
	$Mojo::WebService::Twitter::Util::OAUTH2_BASE_URL = '/oauth2/';
	$api_key = TEST_API_KEY;
	$api_secret = TEST_API_SECRET;
}

my $twitter = Mojo::WebService::Twitter->new(api_key => $api_key, api_secret => $api_secret);;
$twitter->ua->server->app->log->level('warn');

my $request;
ok(eval { $request = $twitter->request_oauth; 1 }, 'requested authorization') or diag $@;
is $request->{oauth_callback_confirmed}, 'true', 'request callback confirmed';<commit_msg>prevent travis from running twitter post tests<commit_after>use Mojolicious::Lite;
use Mojo::Loader 'data_section';
use Time::Piece;
use Test::More;
use Mojo::WebService::Twitter;
use Mojo::WebService::Twitter::Util 'twitter_authorize_url';
use Mojo::Util 'b64_encode';

use constant TEST_API_KEY => 'testapikey';
use constant TEST_API_SECRET => 'testapisecret';
use constant TEST_OAUTH_REQUEST_TOKEN => '<PASSWORD>';
use constant TEST_OAUTH_REQUEST_SECRET => 'testrequestsecret';
use constant TEST_OAUTH_VERIFIER => 'thisisaverifier';
use constant TEST_OAUTH_TOKEN => '<PASSWORD>';
use constant TEST_OAUTH_SECRET => 'testaccesssecret';

app->types->type(form => 'application/x-www-form-urlencoded');

post '/oauth/request_token' => { format => 'form', text => 'oauth_callback_confirmed=true&oauth_token='.TEST_OAUTH_REQUEST_TOKEN.'&oauth_token_secret='.TEST_OAUTH_REQUEST_SECRET };
post '/oauth/access_token' => sub {
	my $c = shift;
	my $oauth_header = $c->req->headers->authorization;
	my ($api_key) = $oauth_header =~ m/oauth_consumer_key="([^"]+)"/;
	is $api_key, TEST_API_KEY, 'received API key';
	my ($verifier) = $oauth_header =~ m/oauth_verifier="([^"]+)"/;
	is $verifier, TEST_OAUTH_VERIFIER, 'received OAuth verifier';
	$c->render(format => 'form', text => 'oauth_token='.TEST_OAUTH_TOKEN.'&oauth_token_secret='.TEST_OAUTH_SECRET);
};
group {
	under '/api' => sub {
		my $c = shift;
		my $oauth_header = $c->req->headers->authorization;
		my ($api_key) = $oauth_header =~ m/oauth_consumer_key="([^"]+)"/;
		is $api_key, TEST_API_KEY, 'received API key';
		my ($access_token) = $oauth_header =~ m/oauth_token="([^"]+)"/;
		is $access_token, TEST_OAUTH_TOKEN, 'received access token';
		return 1;
	};
	get '/account/verify_credentials.json' => { format => 'json', text => data_section('main', 'verify_credentials') };
	post '/statuses/update.json' => { format => 'json', text => data_section('main', 'post_tweet') };
};

my $api_key = $ENV{TWITTER_API_KEY};
my $api_secret = $ENV{TWITTER_API_SECRET};
my $access_token;
my $access_secret;

my $test_online = 0;
if ($ENV{TWITTER_API_POST} and defined $api_key and defined $api_secret) {
	diag 'Running online test for Twitter';
	$test_online = 1;
} else {
	diag 'Running offline test for Twitter; set TWITTER_API_POST and TWITTER_API_KEY/TWITTER_API_SECRET for online test';
	$Mojo::WebService::Twitter::Util::API_BASE_URL = '/api/';
	$Mojo::WebService::Twitter::Util::OAUTH_BASE_URL = '/oauth/';
	$Mojo::WebService::Twitter::Util::OAUTH2_BASE_URL = '/oauth2/';
	$api_key = TEST_API_KEY;
	$api_secret = TEST_API_SECRET;
}

my $twitter = Mojo::WebService::Twitter->new(api_key => $api_key, api_secret => $api_secret);;
$twitter->ua->server->app->log->level('warn');

my $request;
ok(eval { $request = $twitter->request_oauth; 1 }, 'requested authorization') or diag $@;
is $request->{oauth_callback_confirmed}, 'true', 'request callback confirmed';
{'fraction_non_alphanumeric': 0.1717154232300502, 'fraction_numerical': 0.0027696036004846805, 'mean_word_length': 6.83989145183175, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 86, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7435327', 'n_tokens_mistral': 2190, 'n_tokens_neox': 2129, 'n_words': 421}