the browser-facing portion of osu!
at master 3.7 kB view raw
1<?php 2 3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0. 4// See the LICENCE file in the repository root for full licence text. 5 6namespace Tests\Controllers; 7 8use App\Models\User; 9use App\Models\UserClient; 10use Tests\TestCase; 11 12class ClientVerificationsControllerTest extends TestCase 13{ 14 public function testCreate() 15 { 16 $user = User::factory()->create(); 17 18 $hash = implode(':', [md5('osu'), '', md5('mac'), md5('unique'), md5('disk')]); 19 20 $url = route('client-verifications.create', ['ch' => $hash]); 21 22 $this->get($url) 23 ->assertStatus(401) 24 ->assertViewIs('sessions.create'); 25 26 $this->be($user) 27 ->get($url) 28 ->assertStatus(401) 29 ->assertViewIs('users.verify'); 30 31 $this->actingAsVerified($user) 32 ->get($url) 33 ->assertSuccessful() 34 ->assertViewIs('client_verifications.create'); 35 36 UserClient::lookupOrNew($user->getKey(), $hash)->fill(['verified' => true])->save(); 37 38 $this->actingAsVerified($user) 39 ->get($url) 40 ->assertSuccessful() 41 ->assertViewIs('client_verifications.completed'); 42 } 43 44 public function testCreateWithInvalidHash() 45 { 46 $user = User::factory()->create(); 47 48 $this->actingAsVerified($user) 49 ->get(route('client-verifications.create')) 50 ->assertStatus(422); 51 52 $this->actingAsVerified($user) 53 ->get(route('client-verifications.create', ['ch' => 'aa::bb:cc'])) 54 ->assertStatus(422); 55 } 56 57 public function testStore() 58 { 59 $user = User::factory()->create(); 60 61 $hash = implode(':', [md5('osu'), '', md5('mac'), md5('unique'), md5('disk')]); 62 63 $url = route('client-verifications.store', ['ch' => $hash]); 64 65 $initialCount = UserClient::count(); 66 67 $this->post($url) 68 ->assertStatus(401); 69 70 $this->assertSame($initialCount, UserClient::count()); 71 $this->assertFalse(UserClient::lookupOrNew($user->getKey(), $hash)->exists); 72 73 $this->be($user) 74 ->post($url) 75 ->assertStatus(401) 76 ->assertViewIs('users.verify'); 77 78 $this->assertSame($initialCount, UserClient::count()); 79 $this->assertFalse(UserClient::lookupOrNew($user->getKey(), $hash)->exists); 80 81 $returnUrl = route('client-verifications.create', ['ch' => $hash]); 82 83 $this->actingAsVerified($user) 84 ->post($url) 85 ->assertRedirect($returnUrl); 86 87 $this->actingAsVerified($user) 88 ->get($returnUrl) 89 ->assertViewIs('client_verifications.completed'); 90 91 $this->assertSame($initialCount + 1, UserClient::count()); 92 $this->assertTrue(UserClient::lookupOrNew($user->getKey(), $hash)->verified); 93 94 $this->actingAsVerified($user) 95 ->post($url) 96 ->assertRedirect($returnUrl); 97 98 $this->actingAsVerified($user) 99 ->get($returnUrl) 100 ->assertViewIs('client_verifications.completed'); 101 102 $this->assertSame($initialCount + 1, UserClient::count()); 103 } 104 105 public function testStoreWithInvalidHash() 106 { 107 $user = User::factory()->create(); 108 109 $initialCount = UserClient::count(); 110 111 $this->actingAsVerified($user) 112 ->post(route('client-verifications.store')) 113 ->assertStatus(422); 114 115 $this->assertSame($initialCount, UserClient::count()); 116 117 $this->actingAsVerified($user) 118 ->post(route('client-verifications.store', ['ch' => 'aa::bb:cc'])) 119 ->assertStatus(422); 120 121 $this->assertSame($initialCount, UserClient::count()); 122 } 123}