11<?php namespace tests ;
22
33use Atomx \AccountStore ;
4- use Atomx \ApiException ;
4+ use Atomx \Exceptions \ApiException ;
5+ use Atomx \Exceptions \TotpRequiredException ;
56use Atomx \MemoryAccountStore ;
67use Atomx \Resources \Advertiser ;
78use Atomx \Resources \Domain ;
@@ -26,6 +27,7 @@ class TestLoginAccountStore extends MemoryAccountStore {
2627 public function setLoginClient ($ client ) { $ this ->loginClient = $ client ; }
2728 protected function getLoginClient () { return $ this ->loginClient ; }
2829}
30+
2931class ClientTest extends \PHPUnit_Framework_TestCase {
3032 public function testDiscardInvalidToken ()
3133 {
@@ -106,18 +108,51 @@ public function testLogin()
106108 $ loginRequest = $ history ->getIterator ()[0 ]['request ' ];
107109 $ this ->assertArrayNotHasKey ('Authorization ' , $ loginRequest ->getHeaders ());
108110 $ this ->assertArraySubset (['Authorization ' => ['Bearer LOGIN_TOKEN ' ]], $ history ->getLastRequest ()->getHeaders ());
111+ }
112+
113+ public function testTotp ()
114+ {
115+ $ login = new Login (new TestAccountStore );
116+
117+ $ store = new TestLoginAccountStore ();
118+ $ store ->setLoginClient ($ login );
119+
120+ $ advertiser = new Advertiser ($ store );
121+
122+ $ history = new History ();
123+ $ mock = new Mock ([
124+ $ this ->getValidLoginResponse (true ),
125+ $ this ->getValidEmptyResponse ()
126+ ]);
127+
128+ $ login ->getClient ()->getEmitter ()->attach ($ mock );
129+ $ login ->getClient ()->getEmitter ()->attach ($ history );
130+
131+ $ advertiser ->getClient ()->getEmitter ()->attach ($ mock );
132+ $ advertiser ->getClient ()->getEmitter ()->attach ($ history );
133+
134+ $ totpException = false ;
135+
136+ try {
137+ $ advertiser ->get (['limit ' => 1 , 'depth ' => 0 ]);
138+ } catch (TotpRequiredException $ e ) {
139+ $ totpException = true ;
140+ }
109141
142+ $ this ->assertTrue ($ totpException );
143+ $ this ->assertEquals ('TOTP_TOKEN ' , $ store ->getToken ());
110144 }
111145
112146 private function getValidEmptyResponse ()
113147 {
114148 return new Response (200 , [], Stream::factory ("[] " ));
115149 }
116150
117- private function getValidLoginResponse ()
151+ private function getValidLoginResponse ($ totp = false )
118152 {
119- $ loginBody = '{"user":{"id":1},"resource":"auth_token","totp_required":false, ' .
120- '"message":"atomx (user id 1) logged in","success":true,"auth_token":"LOGIN_TOKEN"} ' ;
153+ $ token = $ totp ? 'TOTP_TOKEN ' : 'LOGIN_TOKEN ' ;
154+ $ loginBody = '{"user":{"id":1},"resource":"auth_token","totp_required": ' . var_export ($ totp , true ) . ', ' .
155+ '"message":"atomx (user id 1) logged in","success":true,"auth_token":" ' . $ token . '"} ' ;
121156
122157 return new Response (200 ,
123158 ['Content-Type ' => 'application/json; charset=UTF-8 ' ],
0 commit comments