1use strict;
2use warnings;
3
4use RT::Test::Assets tests => undef;
5
6my $user = RT::Test->load_or_create_user( Name => 'testuser' );
7ok $user->id, "Created user";
8
9my $ticket = RT::Test->create_ticket(
10    Queue   => 1,
11    Subject => 'a test ticket',
12);
13ok $ticket->id, "Created ticket";
14
15my $catalog_one = create_catalog( Name => "One" );
16ok $catalog_one && $catalog_one->id, "Created catalog one";
17
18my $catalog_two = create_catalog( Name => "Two" );
19ok $catalog_two && $catalog_two->id, "Created catalog two";
20
21ok(RT::Test->add_rights({
22    Principal   => 'Privileged',
23    Right       => 'ShowCatalog',
24    Object      => $catalog_one,
25}), "Granted ShowCatalog");
26
27my $asset = RT::Asset->new( RT::CurrentUser->new($user) );
28
29diag "CreateAsset";
30{
31    my %create = (
32        Name    => 'Thinkpad T420s',
33        Contact => 'trs@example.com',
34        Catalog => $catalog_one->id,
35    );
36    my ($id, $msg) = $asset->Create(%create);
37    ok !$id, "Create denied: $msg";
38
39    ok(RT::Test->add_rights({
40        Principal   => 'Privileged',
41        Right       => 'CreateAsset',
42        Object      => $catalog_one,
43    }), "Granted CreateAsset");
44
45    ($id, $msg) = $asset->Create(%create);
46    ok $id, "Created: $msg";
47    is $asset->id, $id, "id matches";
48    is $asset->CatalogObj->Name, $catalog_one->Name, "Catalog matches";
49};
50
51diag "ShowAsset";
52{
53    is $asset->Name, undef, "Can't see Name without ShowAsset";
54    ok !$asset->Contacts->id, "Can't see Contacts role group";
55
56    ok(RT::Test->add_rights({
57        Principal   => 'Privileged',
58        Right       => 'ShowAsset',
59        Object      => $catalog_one,
60    }), "Granted ShowAsset");
61
62    is $asset->Name, "Thinkpad T420s", "Got Name";
63    is $asset->Contacts->UserMembersObj->First->EmailAddress, 'trs@example.com', "Got Contact";
64}
65
66diag "ModifyAsset";
67{
68    my ($txnid, $txnmsg) = $asset->SetName("Lenovo Thinkpad T420s");
69    ok !$txnid, "Update failed: $txnmsg";
70    is $asset->Name, "Thinkpad T420s", "Name didn't change";
71
72    my ($ok, $msg) = $asset->AddLink( Type => 'RefersTo', Target => 't:1' );
73    ok !$ok, "No rights to AddLink: $msg";
74
75    ($ok, $msg) = $asset->DeleteLink( Type => 'RefersTo', Target => 't:1' );
76    ok !$ok, "No rights to DeleteLink: $msg";
77
78    ok(RT::Test->add_rights({
79        Principal   => 'Privileged',
80        Right       => 'ModifyAsset',
81        Object      => $catalog_one,
82    }), "Granted ModifyAsset");
83
84    ($txnid, $txnmsg) = $asset->SetName("Lenovo Thinkpad T420s");
85    ok $txnid, "Updated Name: $txnmsg";
86    is $asset->Name, "Lenovo Thinkpad T420s", "Name changed";
87}
88
89diag "Catalogs";
90{
91    my ($txnid, $txnmsg) = $asset->SetCatalog($catalog_two->id);
92    ok !$txnid, "Failed to update Catalog: $txnmsg";
93    is $asset->CatalogObj->Name, $catalog_one->Name, "Catalog unchanged";
94
95    ok(RT::Test->add_rights({
96        Principal   => 'Privileged',
97        Right       => 'CreateAsset',
98        Object      => $catalog_two,
99    }), "Granted CreateAsset in second catalog");
100
101    ($txnid, $txnmsg) = $asset->SetCatalog($catalog_two->id);
102    ok $txnid, "Updated Catalog: $txnmsg";
103    unlike $txnmsg, qr/Permission Denied/i, "Transaction message isn't Permission Denied";
104    ok !$asset->CurrentUserCanSee, "Can no longer see the asset";
105
106    ok(RT::Test->add_rights({
107        Principal   => 'Privileged',
108        Right       => 'ShowAsset',
109        Object      => $catalog_two,
110    }), "Granted ShowAsset");
111
112    ok $asset->CurrentUserCanSee, "Can see the asset now";
113    is $asset->CatalogObj->Name, undef, "Can't see the catalog name still";
114
115    ok(RT::Test->add_rights({
116        Principal   => 'Privileged',
117        Right       => 'ShowCatalog',
118        Object      => $catalog_two,
119    }), "Granted ShowCatalog");
120
121    is $asset->CatalogObj->Name, $catalog_two->Name, "Now we can see the catalog name";
122}
123
124done_testing;
125