Skip to content

Commit 94ce2a7

Browse files
authored
Merge pull request #1487 from metacpan/metacpan-api-save-profile
Don't force asciiname on author profile update
2 parents 12bcd70 + fcc5c37 commit 94ce2a7

3 files changed

Lines changed: 64 additions & 1 deletion

File tree

lib/MetaCPAN/Document/Author.pm

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,16 @@ sub validate {
137137
my ( $class, $data ) = @_;
138138
my @result;
139139
foreach my $attr ( $class->meta->get_all_attributes ) {
140-
if ( $attr->is_required && !exists $data->{ $attr->name } ) {
140+
141+
# A required attribute with a default (or builder) is still satisfiable
142+
# without input -- the default fills it in at construction time -- so an
143+
# absent value must not be reported as missing. asciiname is the case
144+
# this guards: required => 1 but default => q{}.
145+
if ( $attr->is_required
146+
&& !exists $data->{ $attr->name }
147+
&& !$attr->has_default
148+
&& !$attr->has_builder )
149+
{
141150
push(
142151
@result,
143152
{

t/document/author.t

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,11 @@ my @errors = MetaCPAN::Document::Author->validate(
1010

1111
ok( !( grep { $_->{field} eq 'perlmongers' } @errors ), 'perlmongers ok' );
1212

13+
# asciiname is required => 1 but has a default => q{}, so an absent asciiname
14+
# must not be reported as missing (the default satisfies the requirement).
15+
ok(
16+
!( grep { $_->{field} eq 'asciiname' } @errors ),
17+
'absent asciiname not reported as required'
18+
);
19+
1320
done_testing;

t/server/controller/user/profile.t

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use strict;
2+
use warnings;
3+
use lib 't/lib';
4+
5+
use Cpanel::JSON::XS qw( encode_json );
6+
use MetaCPAN::Server::Test qw( app GET test_psgi );
7+
use MetaCPAN::TestHelpers qw( decode_json_ok );
8+
use Test::More;
9+
10+
use HTTP::Request::Common ();
11+
12+
test_psgi app, sub {
13+
my $cb = shift;
14+
15+
ok( my $res = $cb->( GET '/user/profile?access_token=testing' ),
16+
'GET /user/profile' );
17+
is( $res->code, 200, 'code 200' );
18+
my $profile = decode_json_ok($res);
19+
20+
# An author whose name is already ASCII has an empty/null asciiname. Saving
21+
# the profile form sends "asciiname": null. This must not be rejected with
22+
# "asciiname is required": asciiname is required => 1 but has a default, so
23+
# an absent value is filled in at construction time.
24+
my $put = HTTP::Request::Common::PUT(
25+
'/user/profile?access_token=testing',
26+
Content_Type => 'application/json',
27+
Content => encode_json( {
28+
name => 'Moritz Onken',
29+
asciiname => undef,
30+
website => ['http://metacpan.org/'],
31+
email => ['onken@netcubed.de'],
32+
city => 'Karlsruhe',
33+
region => 'BW',
34+
country => 'DE',
35+
} ),
36+
);
37+
38+
ok( $res = $cb->($put), 'PUT /user/profile with null asciiname' );
39+
is( $res->code, 201, 'profile saved (status created)' )
40+
or diag( $res->content );
41+
42+
my $saved = decode_json_ok($res);
43+
is( $saved->{asciiname} // q{},
44+
q{}, 'asciiname is empty (default applied), not rejected' );
45+
};
46+
47+
done_testing;

0 commit comments

Comments
 (0)