Passed
Push — master ( 7e81b0...7eb007 )
by Robbie
12:39 queued 11s
created

testSetMemberDoesNotResetMethodsWhenNoChange()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 6
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 10
rs 10
1
<?php
2
3
namespace SilverStripe\MFA\Tests\Store;
4
5
use SilverStripe\Dev\SapphireTest;
6
use SilverStripe\MFA\Store\SessionStore;
7
use SilverStripe\ORM\Connect\Database;
8
use SilverStripe\ORM\DB;
9
use SilverStripe\Security\Member;
10
11
class SessionStoreTest extends SapphireTest
12
{
13
    /**
14
     * @expectedException \RuntimeException
15
     * @expectedExceptionMessageRegExp /possibly incorrectly encoded/
16
     */
17
    public function testSerializeThrowsExceptionOnFailure()
18
    {
19
        $store = new SessionStore($this->createMock(Member::class));
20
        $store->setState(['some binary' => random_bytes(32)]);
21
        $store->serialize();
22
    }
23
24
    public function testSetState()
25
    {
26
        $store = new SessionStore($this->createMock(Member::class));
27
        $store->setState(['foo' => 'bar']);
28
        $this->assertSame(['foo' => 'bar'], $store->getState());
29
    }
30
31
    public function testAddState()
32
    {
33
        $store = new SessionStore($this->createMock(Member::class));
34
        $store->setState(['foo' => 'bar', 'bar' => 'baz']);
35
        $store->addState(['foo' => 'baz']);
36
        $this->assertSame(['foo' => 'baz', 'bar' => 'baz'], $store->getState());
37
    }
38
39
    /**
40
     * @expectedException \SilverStripe\MFA\Exception\InvalidMethodException
41
     * @expectedExceptionMessage You cannot verify with a method you have already verified
42
     */
43
    public function testSetMethodWithVerifiedMethod()
44
    {
45
        $store = new SessionStore($this->createMock(Member::class));
46
        $store->addVerifiedMethod('foobar');
47
        $store->setMethod('foobar');
48
    }
49
50
    public function testSetMethod()
51
    {
52
        $store = new SessionStore($this->createMock(Member::class));
53
        $store->setMethod('foobar');
54
        $this->assertSame('foobar', $store->getMethod());
55
    }
56
57
    public function testSetMemberDoesNotResetMethodsWhenNoChange()
58
    {
59
        $member1 = $this->createMock(Member::class);
60
61
        $store = new SessionStore($member1);
62
        $store->setMethod('foobar');
63
        $store->addVerifiedMethod('foobar');
64
        $store->setMember($member1);
65
66
        $this->assertSame('foobar', $store->getMethod());
67
    }
68
69
    public function testSetMemberResetsMethodsWhenMemberChanged()
70
    {
71
        $member1 = new Member();
72
        $member1->ID = 25;
73
        $member2 = new Member();
74
        $member2->ID = 50;
75
76
        $store = new SessionStore($member1);
77
        $store->setMethod('foobar');
78
        $store->addVerifiedMethod('foobar');
79
        $store->setMember($member2);
80
81
        $this->assertEmpty($store->getMethod());
82
    }
83
84
    public function testDatabaseIsNotAccessedOnDeserialise()
85
    {
86
        // Create a store
87
        $member = new Member();
88
        $member->ID = 1;
89
        $store = new SessionStore($member);
90
        $serialised = $store->serialize();
91
92
        // Replace the DB connection with a mock
93
        $connection = DB::get_conn();
94
        $database = $this->getMockBuilder(Database::class)
95
            ->enableProxyingToOriginalMethods()
96
            ->setProxyTarget($connection)
97
            ->getMock();
98
99
        $database->expects($this->never())->method('query');
100
        $database->expects($this->never())->method('preparedQuery');
101
        DB::set_conn($database);
102
103
        // Replicate the deserialisation that happens on session start
104
        $store->unserialize($serialised);
105
106
        // Finish the test and allow mock assertions to fail the test
107
        DB::set_conn($connection);
108
    }
109
}
110