| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Mezon\SocialNetwork\Auth\Tests; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use PHPUnit\Framework\TestCase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @psalm-suppress PropertyNotSetInConstructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | class BaseAuthUnitTest extends TestCase | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * Method returns fake settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |      * @return array fake settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     protected function getSettings(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |         return ([ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |             'client_id' => 1, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |             'client_secret' => 2, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |             'redirect_uri' => 3 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |         ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * Testing constructor | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     public function testConstructor(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |         // setup and test body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |         $auth = new AdoptedBaseAuth($this->getSettings()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |         // assertions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         $this->assertEquals(3, count($auth->getSettings()), 'Setting were not set'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * Testing getLink | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function testGetLink(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         // setup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |         $auth = new AdoptedBaseAuth($this->getSettings()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         // test body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |         $link = $auth->getLink(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |         // assertions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         $this->assertStringContainsString( | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |             'http://oauth-uriclient_id=1&redirect_uri=3&response_type=code', | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             $link, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |             'Invalid link was generated'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Testing getLink exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |     public function testGetLinkException(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         // assertions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $this->expectException(\Exception::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         // setup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         $auth = new AdoptedBaseAuth([]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         // test body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |         $auth->getLink(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * Testing getUserInfoUri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |     public function testGetUserInfoUri(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |         // setup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |         $auth = new AdoptedBaseAuth($this->getSettings()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |         // test body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         $link = $auth->getUserInfoUri(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         // assertions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         $this->assertStringContainsString('://user-info-uri/?', $link, 'Invalid user info URI'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |      * Testing getTokenParams method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |     public function testGetTokenParams(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |         // setup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |         $auth = new AdoptedBaseAuth($this->getSettings()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |         // test body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         $params = $auth->getTokenParams('123'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |         // assertions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $this->assertEquals(1, $params['client_id'], 'Invalid "client_id"'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |         $this->assertEquals(2, $params['client_secret'], 'Invalid "client_secret"'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         $this->assertEquals(3, $params['redirect_uri'], 'Invalid "redirect_uri"'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |         $this->assertEquals(123, $params['code'], 'Invalid "code"'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * Testing getTokenUri | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function testGetTokenUri(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         // setup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |         $auth = new AdoptedBaseAuth($this->getSettings()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         // test body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |         $link = $auth->getTokenUri(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |         // assertions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |         $this->assertStringContainsString('://token-uri', $link, 'Invalid token URI'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * Testing getDesiredFields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |     public function testGetDesiredFields(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |         // setup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |         $auth = new AdoptedBaseAuth($this->getSettings()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |         // test body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         $fields = $auth->getDesiredFields(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |         // assertions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         $this->assertStringContainsString('desired,fields', $fields, 'Invalid token URI'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * Testing 'dispatchUserInfo' method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     public function testDispatchUserInfo(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |         // setup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |         $auth = new AdoptedBaseAuth($this->getSettings()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |         $userInfo = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |             'picture' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |                 'data' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |                     'url' => 'image url' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |                 ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |         // test body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |         $userInfo = $auth->dispatchUserInfo($userInfo); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |         // assertions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |         $this->assertIsString($userInfo['picture'], 'Record was not transformed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |      * Testing 'auth' method | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |     public function testAuth(): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         // setup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |         $auth = new AdoptedBaseAuth($this->getSettings()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |         // test body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |         $result = $auth->auth('some-code'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |         // assertions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |         $this->assertTrue($result, 'Auth was not performed'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * Testing data provider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |      * @return array testing data | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 176 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 177 |  |  |     public function authDataProvider(): array | 
            
                                                                        
                            
            
                                    
            
            
                | 178 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |         return [ | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |             // #0, the first case | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |                 $this->getSettings(), | 
            
                                                                        
                            
            
                                    
            
            
                | 183 |  |  |                 '' | 
            
                                                                        
                            
            
                                    
            
            
                | 184 |  |  |             ], | 
            
                                                                        
                            
            
                                    
            
            
                | 185 |  |  |             // #1, the first case | 
            
                                                                        
                            
            
                                    
            
            
                | 186 |  |  |             [ | 
            
                                                                        
                            
            
                                    
            
            
                | 187 |  |  |                 [], | 
            
                                                                        
                            
            
                                    
            
            
                | 188 |  |  |                 'access-code' | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |             ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |      * Testing 'auth' method when not all data is passed correctly | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |      * @param array $settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |      *            settings | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |      * @param string $code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |      *            access code | 
            
                                                                                                            
                            
            
                                    
            
            
                | 200 |  |  |      * @dataProvider authDataProvider | 
            
                                                                                                            
                            
            
                                    
            
            
                | 201 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 202 |  |  |     public function testAuthFalse(array $settings, string $code): void | 
            
                                                                                                            
                            
            
                                    
            
            
                | 203 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 204 |  |  |         // setup | 
            
                                                                                                            
                            
            
                                    
            
            
                | 205 |  |  |         $auth = new AdoptedBaseAuth($settings); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 206 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 207 |  |  |         // test body | 
            
                                                                                                            
                            
            
                                    
            
            
                | 208 |  |  |         $result = $auth->auth($code); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 209 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 210 |  |  |         // assertions | 
            
                                                                                                            
                            
            
                                    
            
            
                | 211 |  |  |         $this->assertFalse($result); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 212 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 213 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 214 |  |  |  |