add tests

This commit is contained in:
Doug Bell 2010-06-27 21:04:47 -05:00
parent f771e3fcd4
commit 687c36fd27
2 changed files with 107 additions and 2 deletions

View file

@ -164,6 +164,7 @@ Get the Net::Twitter object with the appropriate keys
sub getTwitter {
my ( $self ) = @_;
my $setting = $self->session->setting;
if ( !$self->{_twitter} ) {
my $nt = Net::Twitter->new(
traits => [qw/API::REST OAuth/],
@ -191,14 +192,14 @@ sub www_login {
my $nt = $self->getTwitter;
my $url = $nt->get_authentication_url(
my $auth_url = $nt->get_authentication_url(
callback => $url->getSiteURL . $url->page('op=auth&authType=Twitter&method=callback'),
);
$scratch->set( 'AuthTwitterToken', $nt->request_token );
$scratch->set( 'AuthTwitterTokenSecret', $nt->request_token_secret );
$session->http->setRedirect($url);
$session->http->setRedirect($auth_url);
return "redirect";
}
@ -273,6 +274,9 @@ sub www_setUsername {
my ( $form, $scratch, $db ) = $session->quick(qw( form scratch db ));
my $i18n = WebGUI::International->new( $session, 'Auth_Twitter' );
# Don't allow just anybody to set a username
return unless $scratch->get('AuthTwitterUserId');
my $username = $form->get('newUsername');
if ( !WebGUI::User->newByUsername( $session, $username ) ) {
my $twitterUserId = $scratch->get( "AuthTwitterUserId" );

101
t/Auth/Twitter.t Normal file
View file

@ -0,0 +1,101 @@
# vim:syntax=perl
#-------------------------------------------------------------------
# WebGUI is Copyright 2001-2009 Plain Black Corporation.
#-------------------------------------------------------------------
# Please read the legal notices (docs/legal.txt) and the license
# (docs/license.txt) that came with this distribution before using
# this software.
#------------------------------------------------------------------
# http://www.plainblack.com info@plainblack.com
#------------------------------------------------------------------
# Test the Auth::Twitter module
#
#
use FindBin;
use strict;
use lib "$FindBin::Bin/../lib";
use Test::More;
use WebGUI::Test; # Must use this before any other WebGUI modules
use WebGUI::Session;
#----------------------------------------------------------------------------
# Init
my $session = WebGUI::Test->session;
#----------------------------------------------------------------------------
# Tests
plan tests => 15; # Increment this number for each test you create
#----------------------------------------------------------------------------
# Object creation
use_ok( 'WebGUI::Auth::Twitter' );
my $auth = WebGUI::Auth::Twitter->new( $session, "Twitter" );
isa_ok( $auth, 'WebGUI::Auth::Twitter' );
#----------------------------------------------------------------------------
# API methods
my $user = $auth->createTwitterUser( "1234", "AndyDufresne" );
WebGUI::Test->addToCleanup( $user );
isa_ok( $user, 'WebGUI::User' );
is(
$session->db->quickScalar(
"SELECT fieldData FROM authentication WHERE userId=? AND authMethod=? AND fieldName=?",
[ $user->userId, "Twitter", "twitterUserId" ],
),
"1234",
"Twitter User ID saved in authentication table",
);
my $tmpl = $auth->getTemplateChooseUsername;
isa_ok( $tmpl, 'WebGUI::Asset::Template' );
is( $tmpl->getId, $session->setting->get('twitterTemplateIdChooseUsername'), "Template taken from settings" );
my $nt = $auth->getTwitter;
isa_ok( $nt, 'Net::Twitter' );
#----------------------------------------------------------------------------
# www_ methods
# www_login
is( $auth->www_login, "redirect", "www_login always returns redirect" );
ok( $session->scratch->get('AuthTwitterToken'), 'auth token gets set to scratch' );
ok( $session->scratch->get('AuthTwitterTokenSecret'), 'auth token secret gets set to scratch' );
like( $session->http->getRedirectLocation, qr/twitter[.]com/, "redirect to twitter.com" );
# www_callback
# I have no idea how to test this...
# www_setUsername
ok( !$auth->www_setUsername, "setUsername doesn't work unless a scratch is set" );
$session->scratch->set( 'AuthTwitterUserId' => '2345' );
$session->request->setup_body( {
newUsername => "RedHerring",
} );
$auth->www_setUsername;
# User gets created with given twitter user id
my $userId = $session->db->quickScalar(
"SELECT userId FROM authentication WHERE authMethod=? AND fieldName=? AND fieldData=?",
[ "Twitter", "twitterUserId", "2345" ],
);
ok( $userId, 'user exists in authentication table' );
$user = WebGUI::User->new( $session, $userId );
is( $user->username, "RedHerring", "correct username is set" );
WebGUI::Test->addToCleanup( $user );
like(
$auth->www_setUsername, qr/username "RedHerring" is taken/,
"setUsername with existing username returns error",
);
#vim:ft=perl