aboutsummaryrefslogtreecommitdiffstats
path: root/t/app/model/token.t
blob: 12945975ecb9ef5cc6bdad82c8a80133c3e76e9d (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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#!/usr/bin/perl

use strict;
use warnings;

use Test::More tests => 45;

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";

}