1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use FixMyStreet;
use FixMyStreet::App;
use mySociety::AuthToken;
use mySociety::DBHandle 'dbh';
# set things up so that code using mySociety::DBHandle is happy
FixMyStreet->configure_mysociety_dbhandle();
# NOTE - remember that you need to explicitly dbh()->commit after making
# database changes with the mySociety::* modules.
# create a token using DBIC and check we can read it using AuthToken, and vice
# versa
my %tests = (
nested_hash => { foo => 'bar', and => [ 'baz', 'bundy' ] },
array => [ 'foo', 'bar' ],
scalar => 123,
);
my $token_rs = FixMyStreet::App->model('DB::Token');
# create using DBIC
foreach my $test_data_name ( sort keys %tests ) {
my $test_data = $tests{$test_data_name};
pass "--- testing DBIC create using '$test_data_name'";
my $dbic_token =
$token_rs->create( { scope => 'testing', data => $test_data } );
my $token = $dbic_token->token;
ok $token, "stored token '$token'";
is_deeply $dbic_token->data, $test_data, "data stored correctly using DBIC";
# read back using DBIC
is_deeply $token_rs->find( { token => $token, scope => 'testing' } )->data,
$test_data,
"data read back correctly with DBIC";
# read back using mySociety::AuthToken
is_deeply mySociety::AuthToken::retrieve( 'testing', $token ),
$test_data, "data read back correctly with m::AT";
# delete token
ok $dbic_token->delete, "delete token";
is $token_rs->find( { token => $token, scope => 'testing' } ),
undef,
"token gone for DBIC";
# read back using mySociety::AuthToken
is mySociety::AuthToken::retrieve( 'testing', $token ),
undef, "token gone with m::AT";
}
# create using m::AT
foreach my $test_data_name ( sort keys %tests ) {
my $test_data = $tests{$test_data_name};
pass "--- testing m::AT create using '$test_data_name'";
my $token = mySociety::AuthToken::store( 'testing', $test_data );
dbh->commit();
ok $token, "stored token '$token'";
# read back using DBIC
is_deeply $token_rs->find( { token => $token, scope => 'testing' } )->data,
$test_data,
"data read back correctly with DBIC";
# read back using mySociety::AuthToken
is_deeply mySociety::AuthToken::retrieve( 'testing', $token ),
$test_data, "data read back correctly with m::AT";
# delete token
ok mySociety::AuthToken::destroy( 'testing', $token ), "destroy token";
dbh->commit();
is $token_rs->find( { token => $token, scope => 'testing' } ),
undef,
"token gone for DBIC";
# read back using mySociety::AuthToken
is mySociety::AuthToken::retrieve( 'testing', $token ),
undef, "token gone with m::AT";
}
# Test that the inflation and deflation works as expected
{
my $token =
$token_rs->create( { scope => 'testing', data => {} } );
END { $token->delete() };
# Add in temporary check to test that the data is updated as expected.
is_deeply($token->data, {}, "data is empty");
# store something in it
$token->update({ data => { foo => 'bar' } });
$token->discard_changes();
is_deeply($token->data, { foo => 'bar' }, "data has content");
# change the hash stored
$token->update({ data => { baz => 'bundy' } });
$token->discard_changes();
is_deeply($token->data, { baz => 'bundy' }, "data has new content");
# change the hashref in place
{
my $data = $token->data;
$data->{baz} = 'new';
$token->data( $data );
$token->update();
$token->discard_changes();
is_deeply($token->data, { baz => 'new' }, "data has been updated");
}
# change the hashref in place
{
my $data = $token->data;
$data->{baz} = 'new';
$token->update({ data => $data });
$token->discard_changes();
is_deeply($token->data, { baz => 'new' }, "data has been updated");
}
}
done_testing();
|