From 687c36fd27e9f3379f4eb3b7654c211003ade483 Mon Sep 17 00:00:00 2001 From: Doug Bell Date: Sun, 27 Jun 2010 21:04:47 -0500 Subject: [PATCH] add tests --- lib/WebGUI/Auth/Twitter.pm | 8 ++- t/Auth/Twitter.t | 101 +++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+), 2 deletions(-) create mode 100644 t/Auth/Twitter.t diff --git a/lib/WebGUI/Auth/Twitter.pm b/lib/WebGUI/Auth/Twitter.pm index 9ec89503c..041c5a4da 100644 --- a/lib/WebGUI/Auth/Twitter.pm +++ b/lib/WebGUI/Auth/Twitter.pm @@ -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" ); diff --git a/t/Auth/Twitter.t b/t/Auth/Twitter.t new file mode 100644 index 000000000..73c779bbc --- /dev/null +++ b/t/Auth/Twitter.t @@ -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