Proyectos de Subversion Moodle

Rev

Rev 11 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
// This file is part of Moodle - http://moodle.org/
3
//
4
// Moodle is free software: you can redistribute it and/or modify
5
// it under the terms of the GNU General Public License as published by
6
// the Free Software Foundation, either version 3 of the License, or
7
// (at your option) any later version.
8
//
9
// Moodle is distributed in the hope that it will be useful,
10
// but WITHOUT ANY WARRANTY; without even the implied warranty of
11
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
// GNU General Public License for more details.
13
//
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
16
 
17
namespace enrol_lti\local\ltiadvantage\entity;
18
 
19
use enrol_lti\local\ltiadvantage\repository\legacy_consumer_repository;
20
 
21
/**
22
 * Tests for migration_claim.
23
 *
24
 * @package enrol_lti
25
 * @copyright 2021 Jake Dallimore <jrhdallimore@gmail.com>
26
 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
27
 * @coversDefaultClass \enrol_lti\local\ltiadvantage\entity\migration_claim
28
 */
1441 ariadna 29
final class migration_claim_test extends \advanced_testcase {
1 efrain 30
    /**
31
     * Setup run for each test case.
32
     */
33
    protected function setUp(): void {
1441 ariadna 34
        parent::setUp();
1 efrain 35
        $this->resetAfterTest();
36
    }
37
 
38
    /**
39
     * Returns a stub legacy_consumer_repository, allowing tests to verify claims using a predefined secret.
40
     */
41
    protected function get_stub_legacy_consumer_repo() {
42
        $mockedlegacyconsumerrepo = $this->createStub(legacy_consumer_repository::class);
43
        $mockedlegacyconsumerrepo->method('get_consumer_secrets')
44
            ->willReturn(['consumer_secret']);
45
        return $mockedlegacyconsumerrepo;
46
    }
47
 
48
    /**
49
     * Test instantiation and getters of the migration_claim.
50
     *
51
     * @dataProvider migration_claim_provider
1441 ariadna 52
     * @param array $lti1p1migrationclaim the lti1p1 migration claim.
1 efrain 53
     * @param string $deploymentid string id of the tool deployment.
54
     * @param string $platform string url of the issuer.
55
     * @param string $clientid string id of the client.
56
     * @param string $exp expiry time.
57
     * @param string $nonce nonce.
1441 ariadna 58
     * @param bool $stublegacyconsumerrepo Whether the legacy consumer repo is a stub
1 efrain 59
     * @param array $expected array containing expectation data.
60
     * @covers ::__construct
61
     */
1441 ariadna 62
    public function test_migration_claim(
63
        array $lti1p1migrationclaim,
64
        string $deploymentid,
65
        string $platform,
66
        string $clientid,
67
        string $exp,
68
        string $nonce,
69
        bool $stublegacyconsumerrepo,
70
        array $expected,
71
    ): void {
72
        if ($stublegacyconsumerrepo) {
73
            $legacyconsumerrepo = $this->get_stub_legacy_consumer_repo();
74
        } else {
75
            $legacyconsumerrepo = new legacy_consumer_repository();
76
        }
1 efrain 77
 
78
        if (!empty($expected['exception'])) {
79
            $this->expectException($expected['exception']);
80
            $this->expectExceptionMessage($expected['exceptionmessage']);
1441 ariadna 81
            new migration_claim(
82
                $lti1p1migrationclaim,
83
                $deploymentid,
84
                $platform,
85
                $clientid,
86
                $exp,
87
                $nonce,
88
                $legacyconsumerrepo,
89
            );
1 efrain 90
        } else {
1441 ariadna 91
            $migrationclaim = new migration_claim(
92
                $lti1p1migrationclaim,
93
                $deploymentid,
94
                $platform,
95
                $clientid,
96
                $exp,
97
                $nonce,
98
                $legacyconsumerrepo,
99
            );
1 efrain 100
            $this->assertInstanceOf(migration_claim::class, $migrationclaim);
101
            $this->assertEquals($expected['user_id'], $migrationclaim->get_user_id());
102
            $this->assertEquals($expected['context_id'], $migrationclaim->get_context_id());
1441 ariadna 103
            $this->assertEquals(
104
                $expected['tool_consumer_instance_guid'],
105
                $migrationclaim->get_tool_consumer_instance_guid(),
106
            );
1 efrain 107
            $this->assertEquals($expected['resource_link_id'], $migrationclaim->get_resource_link_id());
108
        }
109
    }
110
 
111
    /**
112
     * Data provider testing migration_claim instantiation.
113
     *
114
     * @return array[] the test case data.
115
     */
1441 ariadna 116
    public static function migration_claim_provider(): array {
1 efrain 117
        // Note: See https://www.imsglobal.org/spec/lti/v1p3/migr#lti-1-1-migration-claim for details regarding the
118
        // correct generation of oauth_consumer_key_sign signature.
119
        return [
120
            'Invalid - missing oauth_consumer_key' => [
121
                'lti1p1migrationclaim' => [
122
                    'oauth_consumer_key' => '',
123
                    'oauth_consumer_key_sign' => 'abcd',
124
                ],
125
                'deploymentid' => 'D12345',
126
                'platform' => 'https://lms.example.org/',
127
                'clientid' => 'a1b2c3d4',
128
                'exp' => '1622612930',
129
                'nonce' => 'j45j2j5nnjn24544',
1441 ariadna 130
                'stublegacyconsumerrepo' => false,
1 efrain 131
                'expected' => [
132
                    'exception' => \coding_exception::class,
133
                    'exceptionmessage' => "Missing 'oauth_consumer_key' property in lti1p1 migration claim."
134
                ]
135
            ],
136
            'Invalid - missing oauth_consumer_key_sign' => [
137
                'lti1p1migrationclaim' => [
138
                    'oauth_consumer_key' => 'CONSUMER_1',
139
                    'oauth_consumer_key_sign' => '',
140
                ],
141
                'deploymentid' => 'D12345',
142
                'platform' => 'https://lms.example.org/',
143
                'clientid' => 'a1b2c3d4',
144
                'exp' => '1622612930',
145
                'nonce' => 'j45j2j5nnjn24544',
1441 ariadna 146
                'stublegacyconsumerrepo' => false,
1 efrain 147
                'expected' => [
148
                    'exception' => \coding_exception::class,
149
                    'exceptionmessage' => "Missing 'oauth_consumer_key_sign' property in lti1p1 migration claim."
150
                ]
151
            ],
152
            'Invalid - incorrect oauth_consumer_key_sign' => [
153
                'lti1p1migrationclaim' => [
154
                    'oauth_consumer_key' => 'CONSUMER_1',
155
                    'oauth_consumer_key_sign' => 'badsignature',
156
                ],
157
                'deploymentid' => 'D12345',
158
                'platform' => 'https://lms.example.org/',
159
                'clientid' => 'a1b2c3d4',
160
                'exp' => '1622612930',
161
                'nonce' => 'j45j2j5nnjn24544',
1441 ariadna 162
                'stublegacyconsumerrepo' => false,
1 efrain 163
                'expected' => [
164
                    'exception' => \coding_exception::class,
165
                    'exceptionmessage' => "Invalid 'oauth_consumer_key_sign' signature in lti1p1 claim."
166
                ]
167
            ],
168
            'Valid - signature valid, map properties not provided' => [
169
                'lti1p1migrationclaim' => [
170
                    'oauth_consumer_key' => 'CONSUMER_1',
171
                    'oauth_consumer_key_sign' => base64_encode(
172
                        hash_hmac(
173
                            'sha256',
174
                            'CONSUMER_1&D12345&https://lms.example.org/&a1b2c3d4&1622612930&j45j2j5nnjn24544',
175
                            'consumer_secret'
176
                        )
177
                    ),
178
                ],
179
                'deploymentid' => 'D12345',
180
                'platform' => 'https://lms.example.org/',
181
                'clientid' => 'a1b2c3d4',
182
                'exp' => '1622612930',
183
                'nonce' => 'j45j2j5nnjn24544',
1441 ariadna 184
                'stublegacyconsumerrepo' => true,
1 efrain 185
                'expected' => [
186
                    'user_id' => null,
187
                    'context_id' => null,
188
                    'tool_consumer_instance_guid' => null,
189
                    'resource_link_id' => null
190
                ]
191
            ],
192
            'Valid - signature valid, map properties are provided' => [
193
                'lti1p1migrationclaim' => [
194
                    'oauth_consumer_key' => 'CONSUMER_1',
195
                    'oauth_consumer_key_sign' => base64_encode(
196
                        hash_hmac(
197
                            'sha256',
198
                            'CONSUMER_1&D12345&https://lms.example.org/&a1b2c3d4&1622612930&j45j2j5nnjn24544',
199
                            'consumer_secret'
200
                        )
201
                    ),
202
                    'user_id' => '24',
203
                    'context_id' => 'd345b',
204
                    'tool_consumer_instance_guid' => '12345-123',
205
                    'resource_link_id' => '4b6fa'
206
                ],
207
                'deploymentid' => 'D12345',
208
                'platform' => 'https://lms.example.org/',
209
                'clientid' => 'a1b2c3d4',
210
                'exp' => '1622612930',
211
                'nonce' => 'j45j2j5nnjn24544',
1441 ariadna 212
                'stublegacyconsumerrepo' => true,
1 efrain 213
                'expected' => [
214
                    'user_id' => '24',
215
                    'context_id' => 'd345b',
216
                    'tool_consumer_instance_guid' => '12345-123',
217
                    'resource_link_id' => '4b6fa'
218
                ]
219
            ]
220
        ];
221
    }
222
}