]> BookStack Code Mirror - bookstack/blob - tests/AuthTest.php
Started Social Auth Testing
[bookstack] / tests / AuthTest.php
1 <?php
2
3 use BookStack\EmailConfirmation;
4
5 class AuthTest extends TestCase
6 {
7
8     public function testAuthWorking()
9     {
10         $this->visit('/')
11             ->seePageIs('/login');
12     }
13
14     public function testLogin()
15     {
16         $this->visit('/')
17             ->seePageIs('/login');
18
19         $this->login('[email protected]', 'password')
20             ->seePageIs('/')
21             ->see('BookStack');
22     }
23
24     public function testPublicViewing()
25     {
26         $settings = app('BookStack\Services\SettingService');
27         $settings->put('app-public', 'true');
28         $this->visit('/')
29             ->seePageIs('/')
30             ->see('Sign In');
31     }
32
33     public function testRegistrationShowing()
34     {
35         // Ensure registration form is showing
36         $this->setSettings(['registration-enabled' => 'true']);
37         $this->visit('/login')
38             ->see('Sign up')
39             ->click('Sign up')
40             ->seePageIs('/register');
41     }
42
43     public function testNormalRegistration()
44     {
45         // Set settings and get user instance
46         $this->setSettings(['registration-enabled' => 'true']);
47         $user = factory(\BookStack\User::class)->make();
48
49         // Test form and ensure user is created
50         $this->visit('/register')
51             ->see('Sign Up')
52             ->type($user->name, '#name')
53             ->type($user->email, '#email')
54             ->type($user->password, '#password')
55             ->press('Create Account')
56             ->seePageIs('/')
57             ->see($user->name)
58             ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email]);
59     }
60
61     public function testConfirmedRegistration()
62     {
63         // Set settings and get user instance
64         $this->setSettings(['registration-enabled' => 'true', 'registration-confirmation' => 'true']);
65         $user = factory(\BookStack\User::class)->make();
66
67         // Mock Mailer to ensure mail is being sent
68         $mockMailer = Mockery::mock('Illuminate\Contracts\Mail\Mailer');
69         $mockMailer->shouldReceive('send')->with('emails/email-confirmation', Mockery::type('array'), Mockery::type('callable'))->twice();
70         $this->app->instance('mailer', $mockMailer);
71
72         // Go through registration process
73         $this->visit('/register')
74             ->see('Sign Up')
75             ->type($user->name, '#name')
76             ->type($user->email, '#email')
77             ->type($user->password, '#password')
78             ->press('Create Account')
79             ->seePageIs('/register/confirm')
80             ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => false]);
81
82         // Test access and resend confirmation email
83         $this->login($user->email, $user->password)
84             ->seePageIs('/register/confirm/awaiting')
85             ->see('Resend')
86             ->visit('/books')
87             ->seePageIs('/register/confirm/awaiting')
88             ->press('Resend Confirmation Email');
89
90         // Get confirmation
91         $user = $user->where('email', '=', $user->email)->first();
92         $emailConfirmation = EmailConfirmation::where('user_id', '=', $user->id)->first();
93
94
95         // Check confirmation email button and confirmation activation.
96         $this->visit('/register/confirm/' . $emailConfirmation->token . '/email')
97             ->see('Email Confirmation')
98             ->click('Confirm Email')
99             ->seePageIs('/')
100             ->see($user->name)
101             ->notSeeInDatabase('email_confirmations', ['token' => $emailConfirmation->token])
102             ->seeInDatabase('users', ['name' => $user->name, 'email' => $user->email, 'email_confirmed' => true]);
103     }
104
105     public function testLogout()
106     {
107         $this->asAdmin()
108             ->visit('/')
109             ->seePageIs('/')
110             ->visit('/logout')
111             ->visit('/')
112             ->seePageIs('/login');
113     }
114
115     /**
116      * Perform a login
117      * @param string $email
118      * @param string $password
119      * @return $this
120      */
121     private function login($email, $password)
122     {
123         return $this->visit('/login')
124             ->type($email, '#email')
125             ->type($password, '#password')
126             ->press('Sign In');
127     }
128 }