Selaa lähdekoodia

Switch to UserInfoRequestedEvent in qauth.

pull/407/head
Greg Holmes 9 vuotta sitten
vanhempi
commit
f202f8a056

+ 11
- 15
qauth/src/com/dmdirc/addons/qauth/QAuthManager.java Näytä tiedosto

@@ -35,10 +35,11 @@ import com.dmdirc.events.QueryMessageEvent;
35 35
 import com.dmdirc.events.ServerConnectedEvent;
36 36
 import com.dmdirc.events.ServerInviteReceivedEvent;
37 37
 import com.dmdirc.events.ServerNoticeEvent;
38
-import com.dmdirc.events.ServerNumericEvent;
38
+import com.dmdirc.events.UserInfoResponseEvent;
39 39
 import com.dmdirc.interfaces.Connection;
40 40
 import com.dmdirc.interfaces.User;
41 41
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
42
+import com.dmdirc.parser.events.UserInfoEvent;
42 43
 import com.dmdirc.plugins.PluginDomain;
43 44
 import com.dmdirc.plugins.PluginInfo;
44 45
 import com.dmdirc.util.validators.NotEmptyValidator;
@@ -64,7 +65,6 @@ public class QAuthManager {
64 65
     private boolean autoInvite;
65 66
     private boolean acceptInvites;
66 67
     private boolean waitingWhois;
67
-    private boolean authed;
68 68
 
69 69
     @Inject
70 70
     public QAuthManager(
@@ -113,7 +113,6 @@ public class QAuthManager {
113 113
         connection.getLocalUser().ifPresent(u -> {
114 114
             connection.requestUserInfo(u);
115 115
             waitingWhois = true;
116
-            authed = false;
117 116
         });
118 117
     }
119 118
 
@@ -130,22 +129,19 @@ public class QAuthManager {
130 129
     }
131 130
 
132 131
     @Handler
133
-    void handleServerNumericEvent(final ServerNumericEvent event) {
132
+    void handleUserInfoResponse(final UserInfoResponseEvent event) {
134 133
         if (!waitingWhois) {
135 134
             return;
136 135
         }
137
-        if(event.getNumeric() == 330 && event.getConnection().getLocalUser().map(User::getNickname)
138
-                .orElse("").equalsIgnoreCase(event.getArgs()[3])) {
139
-            // TODO: Check account matches? (param arg 4)
140
-            authed = true;
141
-        }
142
-        if (event.getNumeric() == 318 && event.getConnection().getLocalUser().map(User::getNickname)
143
-                .orElse("").equalsIgnoreCase(event.getArgs()[3])) {
144
-            waitingWhois = false;
145
-            if (!authed) {
146
-                auth(event.getConnection());
136
+        event.getConnection().getLocalUser().ifPresent(u -> {
137
+            if (u.equals(event.getUser())) {
138
+                // TODO: Check account matches?
139
+                if (!event.getInfo(UserInfoEvent.UserInfoType.ACCOUNT_NAME).isPresent()) {
140
+                    auth(event.getConnection());
141
+                }
142
+                waitingWhois = false;
147 143
             }
148
-        }
144
+        });
149 145
     }
150 146
 
151 147
     @Handler

+ 32
- 1
qauth/test/com/dmdirc/addons/qauth/QAuthManagerTest.java Näytä tiedosto

@@ -33,10 +33,12 @@ import com.dmdirc.events.QueryMessageEvent;
33 33
 import com.dmdirc.events.ServerConnectedEvent;
34 34
 import com.dmdirc.events.ServerInviteReceivedEvent;
35 35
 import com.dmdirc.events.ServerNoticeEvent;
36
+import com.dmdirc.events.UserInfoResponseEvent;
36 37
 import com.dmdirc.interfaces.Connection;
37 38
 import com.dmdirc.interfaces.User;
38 39
 import com.dmdirc.interfaces.config.AggregateConfigProvider;
39 40
 import com.dmdirc.interfaces.config.ConfigProvider;
41
+import com.dmdirc.parser.events.UserInfoEvent;
40 42
 import com.dmdirc.plugins.PluginInfo;
41 43
 import com.dmdirc.plugins.PluginMetaData;
42 44
 
@@ -54,6 +56,7 @@ import static org.junit.Assert.assertTrue;
54 56
 import static org.mockito.Matchers.anyString;
55 57
 import static org.mockito.Mockito.never;
56 58
 import static org.mockito.Mockito.verify;
59
+import static org.mockito.Mockito.verifyNoMoreInteractions;
57 60
 import static org.mockito.Mockito.verifyZeroInteractions;
58 61
 import static org.mockito.Mockito.when;
59 62
 
@@ -79,6 +82,7 @@ public class QAuthManagerTest {
79 82
     @Mock private Invite invite;
80 83
     @Mock private Connection connection;
81 84
     @Mock private PreferencesCategory preferencesCategory;
85
+    @Mock private UserInfoResponseEvent userInfoResponseEvent;
82 86
     @Captor private ArgumentCaptor<PreferencesCategory> preferencesCategoryArgumentCaptor;
83 87
     private QAuthManager instance;
84 88
 
@@ -107,6 +111,7 @@ public class QAuthManagerTest {
107 111
         when(preferencesDialogModel.getConfigManager()).thenReturn(aggregateConfigProvider);
108 112
         when(preferencesDialogModel.getIdentity()).thenReturn(configProvider);
109 113
         when(preferencesDialogModel.getCategory("Plugins")).thenReturn(preferencesCategory);
114
+        when(userInfoResponseEvent.getConnection()).thenReturn(connection);
110 115
         instance = new QAuthManager("pluginDomain", pluginInfo, config, eventBus);
111 116
 
112 117
         instance.handleUsername("username");
@@ -256,5 +261,31 @@ public class QAuthManagerTest {
256 261
         assertTrue(preferencesCategoryArgumentCaptor.getValue().getSettings().size() > 1);
257 262
     }
258 263
 
259
-    // TODO: Test ServerNumericEvent method
264
+    @Test
265
+    public void testWhoisReply_NotWaiting() throws Exception {
266
+        instance.handleUserInfoResponse(userInfoResponseEvent);
267
+        verifyNoMoreInteractions(connection);
268
+    }
269
+
270
+    @Test
271
+    public void testWhoisReply_Waiting_Authed() throws Exception {
272
+        when(userInfoResponseEvent.getInfo(UserInfoEvent.UserInfoType.ACCOUNT_NAME))
273
+                .thenReturn(Optional.of("RAR"));
274
+        when(userInfoResponseEvent.getUser()).thenReturn(localUser);
275
+        instance.handleWhois(true);
276
+        instance.handleConnect(serverConnectedEvent);
277
+        instance.handleUserInfoResponse(userInfoResponseEvent);
278
+        verify(connection, never()).sendMessage(anyString(), anyString());
279
+    }
280
+
281
+    @Test
282
+    public void testWhoisReply_Waiting_NotAuthed() throws Exception {
283
+        when(userInfoResponseEvent.getInfo(UserInfoEvent.UserInfoType.ACCOUNT_NAME))
284
+                .thenReturn(Optional.empty());
285
+        when(userInfoResponseEvent.getUser()).thenReturn(localUser);
286
+        instance.handleWhois(true);
287
+        instance.handleConnect(serverConnectedEvent);
288
+        instance.handleUserInfoResponse(userInfoResponseEvent);
289
+        verify(connection).sendMessage(anyString(), anyString());
290
+    }
260 291
 }

Loading…
Peruuta
Tallenna