GDPR-IAB-TCFv2

 view release on metacpan or  search on metacpan

t/03-json-range.t  view on Meta::CPAN

use strict;
use warnings;

use Test::More;

use GDPR::IAB::TCFv2;

# use JSON;
# use DateTime;
# use DateTimeX::TO_JSON formatter => 'DateTime::Format::RFC3339';

subtest "range" => sub {
    subtest
      "should convert data to json using compact flag and 0/1 as booleans" =>
      sub {
        subtest "should convert data to json using yyyymmdd as date format" =>
          sub {
            my $consent = GDPR::IAB::TCFv2->Parse(
                'COyfVVoOyfVVoADACHENAwCAAAAAAAAAAAAAE5QBgALgAqgD8AQACSwEygJyAnSAMABgAFkAgQCDASeAmYBOgAA',
                json => {
                    verbose        => 0,
                    compact        => 1,
                    use_epoch      => 0,
                    boolean_values => [ 0, 1 ],
                    date_format    => '%Y%m%d',    # yyymmdd
                },
            );

            my $got      = $consent->TO_JSON();
            my $expected = _fixture_range_compact(
                created      => 20200427,
                last_updated => 20200427,
            );

            is_deeply $got, $expected, "must return the json hashref";

            done_testing;
          };

        subtest "should convert data to json using epoch date format" => sub {
            my $consent = GDPR::IAB::TCFv2->Parse(
                'COyfVVoOyfVVoADACHENAwCAAAAAAAAAAAAAE5QBgALgAqgD8AQACSwEygJyAnSAMABgAFkAgQCDASeAmYBOgAA',
                json => {
                    verbose        => 0,
                    compact        => 1,
                    use_epoch      => 1,
                    boolean_values => [ 0, 1 ],
                },
            );


            my $got      = $consent->TO_JSON();
            my $expected = _fixture_range_compact(
                created      => 1587946020,
                last_updated => 1587946020,
            );

            is_deeply $got, $expected, "must return the json hashref";

            done_testing;
        };

        done_testing;
      };

    subtest
      "should convert data to json using default (non-compact) and 0/1 as booleans"
      => sub {

        subtest "default non verbose, date as iso 8601" => sub {
            my $consent = GDPR::IAB::TCFv2->Parse(
                'COyfVVoOyfVVoADACHENAwCAAAAAAAAAAAAAE5QBgALgAqgD8AQACSwEygJyAnSAMABgAFkAgQCDASeAmYBOgAA',
                json => {
                    verbose        => 0,
                    compact        => 0,
                    use_epoch      => 0,
                    boolean_values => [ 0, 1 ],
                },
            );

            ok $consent->vendor_consent(23);

            my $got      = $consent->TO_JSON();
            my $expected = _fixture_range_default();

            is_deeply $got, $expected, "must return the json hashref";

            done_testing;
        };

        subtest "default verbose, date as iso 8601" => sub {
            my $consent = GDPR::IAB::TCFv2->Parse(
                'COyfVVoOyfVVoADACHENAwCAAAAAAAAAAAAAE5QBgALgAqgD8AQACSwEygJyAnSAMABgAFkAgQCDASeAmYBOgAA',
                json => {
                    verbose        => 1,
                    compact        => 0,
                    use_epoch      => 0,
                    boolean_values => [ 0, 1 ],
                },
            );

            ok $consent->vendor_consent(23);

            my $got      = $consent->TO_JSON();
            my $expected = _fixture_range_verbose();

            is_deeply $got, $expected, "must return the json hashref";

            done_testing;
        };

        done_testing;
      };

    subtest "publisher section" => sub {
        subtest "publisher section without publisher_tc" => sub {
            my $consent = GDPR::IAB::TCFv2->Parse(
                'COwAdDhOwAdDhN4ABAENAPCgAAQAAv___wAAAFP_AAp_4AI6ACACAA',
                json => {
                    verbose        => 0,
                    compact        => 1,
                    use_epoch      => 0,
                    boolean_values => [ 0, 1 ],
                },
            );

            my $got = $consent->TO_JSON;
            my $expected =
              { "publisher" => { "restrictions" => { "7" => { "32" => 1 } } }
              };

            is_deeply $got->{publisher}, $expected->{publisher},
              "must return the same publisher restriction section";

            done_testing;
        };
        subtest "publisher section with publisher_tc" => sub {
            subtest "without custom purposes" => sub {
                my $consent = GDPR::IAB::TCFv2->Parse(
                    'COwAdDhOwAdDhN4ABAENAPCgAAQAAv___wAAAFP_AAp_4AI6ACACAA.argAC0gAAAAAAAAAAAA',
                    json => {
                        verbose        => 0,
                        compact        => 1,
                        use_epoch      => 0,
                        boolean_values => [ 0, 1 ],
                    },
                );

                my $got      = $consent->TO_JSON;
                my $expected = {
                    "publisher" => {
                        "consents"             => [ 2, 4, 6, 8, 9, 10 ],
                        "legitimate_interests" => [ 2, 4, 5, 7, 10 ],
                        "custom_purposes"      => {
                            "consents"             => [],
                            "legitimate_interests" => [],
                        },
                        "restrictions" => { "7" => { "32" => 1 } }
                    }
                };

                is_deeply $got->{publisher}, $expected->{publisher},
                  "must return the same publisher restriction section";

                done_testing;
            };

            subtest "with custom purposes" => sub {
                my $consent = GDPR::IAB::TCFv2->Parse(
                    'COwAdDhOwAdDhN4ABAENAPCgAAQAAv___wAAAFP_AAp_4AI6ACACAA.YAAAAAAAAXA',
                    json => {
                        verbose        => 0,
                        compact        => 1,
                        use_epoch      => 0,
                        boolean_values => [ 0, 1 ],
                    },
                );

                my $got      = $consent->TO_JSON;
                my $expected = {
                    "publisher" => {
                        "consents"             => [],
                        "legitimate_interests" => [],
                        "custom_purposes"      => {
                            "consents"             => [ 1, 2 ],
                            "legitimate_interests" => [1],
                        },
                        "restrictions" => { "7" => { "32" => 1 } }
                    }
                };

                is_deeply $got->{publisher}, $expected->{publisher},
                  "must return the same publisher restriction section";

                done_testing;
            };

            done_testing;
        };

        done_testing;
    };

    subtest "TO_JSON method should return the same hashref " => sub {
        my $consent = GDPR::IAB::TCFv2->Parse(
            'COyfVVoOyfVVoADACHENAwCAAAAAAAAAAAAAE5QBgALgAqgD8AQACSwEygJyAnSAMABgAFkAgQCDASeAmYBOgAA',
            json => {
                verbose        => 0,
                compact        => 1,
                use_epoch      => 0,
                boolean_values => [ 0, 1 ],
            },
        );


        my $got1 = $consent->TO_JSON();
        my $got2 = $consent->TO_JSON();

        is_deeply $got1, $got2, "must return the same hashref";

        done_testing;
    };

    done_testing;

};

done_testing;

sub _fixture_range_compact {
    my (%extra) = @_;

    return {
        'tc_string' =>
          'COyfVVoOyfVVoADACHENAwCAAAAAAAAAAAAAE5QBgALgAqgD8AQACSwEygJyAnSAMABgAFkAgQCDASeAmYBOgAA',
        'consent_language' => 'EN',
        'purpose'          => {
            'consents'             => [],
            'legitimate_interests' => []
        },
        'cmp_id'                  => 3,
        'purpose_one_treatment'   => 0,
        'publisher'               => { 'restrictions' => {} },
        'special_features_opt_in' => [],
        'last_updated'            => '20200427',
        'use_non_standard_stacks' => 0,
        'policy_version'          => 2,
        'version'                 => 2,
        'is_service_specific'     => 0,
        'created'                 => '20200427',
        'consent_screen'          => 7,
        'vendor_list_version'     => 48,
        'cmp_version'             => 2,
        'publisher_country_code'  => 'AA',
        'vendor'                  => {
            'consents' => [
                23,
                42,
                126,
                127,
                128,
                587,
                613,
                626
            ],
            'legitimate_interests' => [
                24,
                44,
                129,
                130,
                131,



( run in 2.361 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )