aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/model/token.t
blob: 60b170a26059c4e085cb6d6f89effb846a6fbd06 (plain)
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
use FixMyStreet::Test;

use FixMyStreet;
use FixMyStreet::DB;

my %tests = (
    nested_hash => { foo => 'bar', and => [ 'baz', 'bundy' ] },
    array  => [ 'foo', 'bar' ],
    scalar => 123,
);

my $token_rs = FixMyStreet::DB->resultset('Token');

foreach my $test_data_name ( sort keys %tests ) {
    my $test_data = $tests{$test_data_name};

    pass "--- testing token creation 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";

    # read back from database
    is_deeply $token_rs->find( { token => $token, scope => 'testing' } )->data,
      $test_data,
      "data read back correctly";

    # delete token
    ok $dbic_token->delete, "delete token";

    is $token_rs->find( { token => $token, scope => 'testing' } ),
      undef,
      "token gone";
}

# 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();