fu: Close-up of Fu, bringing a scoop of water to her mouth (Default)
fu ([personal profile] fu) wrote in [site community profile] changelog2011-06-16 11:36 am

[dw-free] Usernames from non-LJ based sites are treated like they're from LJ-based sites

[commit: http://hg.dwscoalition.org/dw-free/rev/d74542fefe8e]

http://bugs.dwscoalition.org/show_bug.cgi?id=3738

Different rules for LJ-based and non-LJ-based sites.

Patch by [personal profile] fu.

Files modified:
  • cgi-bin/DW/External/Site.pm
  • cgi-bin/DW/External/Site/DeadJournal.pm
  • cgi-bin/DW/External/Site/Dreamwidth.pm
  • cgi-bin/DW/External/Site/InsaneJournal.pm
  • cgi-bin/DW/External/Site/JournalFen.pm
  • cgi-bin/DW/External/Site/LiveJournal.pm
  • cgi-bin/DW/External/User.pm
  • t/external-user.t
--------------------------------------------------------------------------------
diff -r ce3f5d35d0c3 -r d74542fefe8e cgi-bin/DW/External/Site.pm
--- a/cgi-bin/DW/External/Site.pm	Thu Jun 16 19:05:32 2011 +0800
+++ b/cgi-bin/DW/External/Site.pm	Thu Jun 16 19:36:18 2011 +0800
@@ -151,5 +151,15 @@
     return $_[0]->{servicetype};
 }
 
+# returns a cleaned version of the username
+sub canonical_username {
+    my $input = $_[1];
+    my $user = "";
+
+    if ( $input =~ /^\s*([a-zA-Z0-9_\-]+)\s*$/ ) {  # good username
+        $user = $1;
+    }
+    return $user;
+}
 
 1;
diff -r ce3f5d35d0c3 -r d74542fefe8e cgi-bin/DW/External/Site/DeadJournal.pm
--- a/cgi-bin/DW/External/Site/DeadJournal.pm	Thu Jun 16 19:05:32 2011 +0800
+++ b/cgi-bin/DW/External/Site/DeadJournal.pm	Thu Jun 16 19:36:18 2011 +0800
@@ -86,4 +86,8 @@
     return $req;
 }
 
+sub canonical_username {
+    return LJ::canonical_username( $_[1] );
+}
+
 1;
diff -r ce3f5d35d0c3 -r d74542fefe8e cgi-bin/DW/External/Site/Dreamwidth.pm
--- a/cgi-bin/DW/External/Site/Dreamwidth.pm	Thu Jun 16 19:05:32 2011 +0800
+++ b/cgi-bin/DW/External/Site/Dreamwidth.pm	Thu Jun 16 19:36:18 2011 +0800
@@ -73,5 +73,8 @@
     return "$LJ::IMGPREFIX/silk/identity/user.png";
 }
 
+sub canonical_username {
+    return LJ::canonical_username( $_[1] );
+}
 
 1;
diff -r ce3f5d35d0c3 -r d74542fefe8e cgi-bin/DW/External/Site/InsaneJournal.pm
--- a/cgi-bin/DW/External/Site/InsaneJournal.pm	Thu Jun 16 19:05:32 2011 +0800
+++ b/cgi-bin/DW/External/Site/InsaneJournal.pm	Thu Jun 16 19:36:18 2011 +0800
@@ -84,4 +84,8 @@
     return $req;
 }
 
+sub canonical_username {
+    return LJ::canonical_username( $_[1] );
+}
+
 1;
diff -r ce3f5d35d0c3 -r d74542fefe8e cgi-bin/DW/External/Site/JournalFen.pm
--- a/cgi-bin/DW/External/Site/JournalFen.pm	Thu Jun 16 19:05:32 2011 +0800
+++ b/cgi-bin/DW/External/Site/JournalFen.pm	Thu Jun 16 19:36:18 2011 +0800
@@ -73,5 +73,8 @@
     return 'http://www.journalfen.net/img/userinfo.gif';
 }
 
+sub canonical_username {
+    return LJ::canonical_username( $_[1] );
+}
 
 1;
diff -r ce3f5d35d0c3 -r d74542fefe8e cgi-bin/DW/External/Site/LiveJournal.pm
--- a/cgi-bin/DW/External/Site/LiveJournal.pm	Thu Jun 16 19:05:32 2011 +0800
+++ b/cgi-bin/DW/External/Site/LiveJournal.pm	Thu Jun 16 19:36:18 2011 +0800
@@ -73,5 +73,8 @@
     return "$LJ::IMGPREFIX/external/lj-userinfo.gif";
 }
 
+sub canonical_username {
+    return LJ::canonical_username( $_[1] );
+}
 
 1;
diff -r ce3f5d35d0c3 -r d74542fefe8e cgi-bin/DW/External/User.pm
--- a/cgi-bin/DW/External/User.pm	Thu Jun 16 19:05:32 2011 +0800
+++ b/cgi-bin/DW/External/User.pm	Thu Jun 16 19:36:18 2011 +0800
@@ -36,7 +36,7 @@
     my $ext = DW::External::Site->get_site( site => $site )
         or return undef;
 
-    my $vuser = LJ::canonical_username($user)
+    my $vuser = $ext->canonical_username( $user )
         or return undef;
 
     my $self = {
diff -r ce3f5d35d0c3 -r d74542fefe8e t/external-user.t
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/t/external-user.t	Thu Jun 16 19:36:18 2011 +0800
@@ -0,0 +1,101 @@
+# -*-perl-*-
+
+use strict;
+use Test::More tests => 14;
+
+use lib "$ENV{LJHOME}/cgi-bin";
+require 'ljlib.pl';
+
+use DW::External::User;
+
+note( "Username with capital letters" );
+{
+    my $u = DW::External::User->new(
+        user => "ExampleUsername",
+        site => "twitter.com"
+    );
+
+    is( $u->site->{hostname}, "twitter.com", "Site is twitter.com" );
+    is( $u->user, "ExampleUsername", "Keep capital letters" );
+}
+
+note( "Username with capital letters (LJ-based site)" );
+{
+    my $u = DW::External::User->new(
+        user => "ExampleUsername",
+        site => "livejournal.com"
+    );
+
+    is( $u->site->{hostname}, "www.livejournal.com", "Site is livejournal.com" );
+    is( $u->user, "exampleusername", "Lowercase this" );
+}
+
+
+note( "Username with spaces" );
+{
+    my $u = DW::External::User->new(
+        user => " exampleusername    ",
+        site => "twitter.com"
+    );
+
+    is( $u->site->{hostname}, "twitter.com", "Site is twitter.com" );
+    is( $u->user, "exampleusername", "Ignore spaces" );
+}
+
+note( "Username with spaces (LJ-based site)" );
+{
+    my $u = DW::External::User->new(
+        user => " exampleusername    ",
+        site => "livejournal.com"
+    );
+
+    is( $u->site->{hostname}, "www.livejournal.com", "Site is livejournal.com" );
+    is( $u->user, "exampleusername", "Ignore spaces" );
+}
+
+
+note( "Username with non-alphanumeric punctuation" );
+{
+    my $u = DW::External::User->new(
+        user => "<exampleusername>",
+        site => "twitter.com"
+    );
+
+    is( $u, undef, "Looks weird. Reject it" );
+}
+
+note( "Username with non-alphanumeric punctuation (LJ-based site)" );
+{
+    my $u = DW::External::User->new(
+        user => "<exampleusername>",
+        site => "livejournal.com"
+    );
+
+    is( $u, undef, "Looks weird. Reject it" );
+}
+
+
+note( "Username with hyphen" );
+{
+    my $u = DW::External::User->new(
+        user => "example-username",
+        site => "twitter.com"
+    );
+
+    is( $u->site->{hostname}, "twitter.com", "Site is twitter.com" );
+    is( $u->user, "example-username", "Hyphens are ok" );
+}
+
+note( "Username with hyphen (LJ-based site)" );
+{
+    my $u = DW::External::User->new(
+        user => "example-username",
+        site => "livejournal.com"
+    );
+
+    is( $u->user, "example_username", "Canonicalize usernames from LJ-based sites" );
+    is( $u->site->{hostname}, "www.livejournal.com", "Site is livejournal.com" );
+}
+
+1;
+
--------------------------------------------------------------------------------