22
33import static org .mockito .Matchers .any ;
44import static org .mockito .Matchers .eq ;
5- import static org .mockito .Mockito .doNothing ;
65import static org .mockito .Mockito .mock ;
76import static org .mockito .Mockito .spy ;
87import static org .mockito .Mockito .when ;
98import static org .powermock .api .mockito .PowerMockito .mockStatic ;
9+ import static org .powermock .api .mockito .PowerMockito .verifyStatic ;
1010
1111import fi .helsinki .cs .tmc .cli .Application ;
1212import fi .helsinki .cs .tmc .cli .CliContext ;
1313import fi .helsinki .cs .tmc .cli .io .TestIo ;
14+ import fi .helsinki .cs .tmc .cli .tmcstuff .CourseInfo ;
1415import fi .helsinki .cs .tmc .cli .tmcstuff .Settings ;
1516import fi .helsinki .cs .tmc .cli .tmcstuff .SettingsIo ;
1617import fi .helsinki .cs .tmc .cli .tmcstuff .TmcUtil ;
1718import fi .helsinki .cs .tmc .core .TmcCore ;
19+ import fi .helsinki .cs .tmc .core .configuration .TmcSettings ;
1820
1921import org .junit .Before ;
2022import org .junit .Test ;
2123import org .junit .runner .RunWith ;
24+
2225import org .powermock .core .classloader .annotations .PrepareForTest ;
2326import org .powermock .modules .junit4 .PowerMockRunner ;
2427
@@ -49,7 +52,17 @@ public void setUp() {
4952 }
5053
5154 @ Test
52- public void logsInWithCorrectServerUserAndPassword () throws Exception {
55+ public void failIfBackendFails () {
56+ app = new Application (ctx );
57+ when (ctx .loadBackendWithoutLogin ()).thenReturn (false );
58+
59+ String [] args = {"login" , "-s" , SERVER , "-u" , USERNAME , "-p" , PASSWORD };
60+ app .run (args );
61+ io .assertNotContains ("Login successful" );
62+ }
63+
64+ @ Test
65+ public void logsInWithCorrectServerUserAndPassword () {
5366 when (TmcUtil .tryToLogin (eq (ctx ), any (Settings .class ))).thenReturn (true );
5467 when (SettingsIo .save (any (Settings .class ))).thenReturn (true );
5568 String [] args = {"login" , "-s" , SERVER , "-u" , USERNAME , "-p" , PASSWORD };
@@ -58,16 +71,16 @@ public void logsInWithCorrectServerUserAndPassword() throws Exception {
5871 }
5972
6073 @ Test
61- public void userGetsErrorMessageIfLoginFails () throws Exception {
74+ public void userGetsErrorMessageIfLoginFails () {
6275 when (TmcUtil .tryToLogin (eq (ctx ), any (Settings .class ))).thenReturn (true );
6376 when (SettingsIo .save (any (Settings .class ))).thenReturn (false );
6477 String [] args = {"login" , "-s" , SERVER , "-u" , USERNAME , "-p" , "WrongPassword" };
6578 app .run (args );
66- io .assertContains ("Login failed ." );
79+ io .assertContains ("Failed to write the accounts file ." );
6780 }
6881
6982 @ Test
70- public void loginAsksUsernameFromUserIfNotGiven () throws Exception {
83+ public void loginAsksUsernameFromUserIfNotGiven () {
7184 when (TmcUtil .tryToLogin (eq (ctx ), any (Settings .class ))).thenReturn (true );
7285 String [] args = {"login" , "-s" , SERVER , "-p" , PASSWORD };
7386 io .addLinePrompt (USERNAME );
@@ -76,7 +89,7 @@ public void loginAsksUsernameFromUserIfNotGiven() throws Exception {
7689 }
7790
7891 @ Test
79- public void loginAsksPasswordFromUserIfNotGiven () throws Exception {
92+ public void loginAsksPasswordFromUserIfNotGiven () {
8093 when (TmcUtil .tryToLogin (eq (ctx ), any (Settings .class ))).thenReturn (true );
8194 String [] args = {"login" , "-s" , SERVER , "-u" , USERNAME };
8295 io .addPasswordPrompt (PASSWORD );
@@ -85,11 +98,38 @@ public void loginAsksPasswordFromUserIfNotGiven() throws Exception {
8598 }
8699
87100 @ Test
88- public void loginAsksServerFromUserIfNotGiven () throws Exception {
101+ public void loginAsksServerFromUserIfNotGiven () {
89102 when (TmcUtil .tryToLogin (eq (ctx ), any (Settings .class ))).thenReturn (true );
90103 String [] args = {"login" , "-p" , PASSWORD , "-u" , USERNAME };
91104 io .addLinePrompt (SERVER );
92105 app .run (args );
93106 io .assertAllPromptsUsed ();
94107 }
108+
109+ @ Test
110+ public void serverAndNotAskedAfterLogout () {
111+ TmcSettings settings = new Settings (SERVER , "username" , "pass" );
112+ CourseInfo info = new CourseInfo (settings , null );
113+ when (TmcUtil .tryToLogin (eq (ctx ), any (Settings .class ))).thenReturn (true );
114+ when (ctx .getCourseInfo ()).thenReturn (info );
115+ String [] args = {"login" };
116+ io .addPasswordPrompt (PASSWORD );
117+ app .run (args );
118+ io .assertAllPromptsUsed ();
119+ }
120+
121+ @ Test
122+ public void courseInfoValuesOverridedByOptions () {
123+ Settings settings = new Settings (SERVER , "username" , "pass" );
124+ CourseInfo info = new CourseInfo (settings , null );
125+ when (TmcUtil .tryToLogin (eq (ctx ), any (Settings .class ))).thenReturn (true );
126+ when (ctx .getCourseInfo ()).thenReturn (info );
127+ String [] args = {"login" , "-p" , PASSWORD , "-u" , USERNAME };
128+ app .run (args );
129+ io .assertAllPromptsUsed ();
130+
131+ Settings expectedSettings = new Settings (SERVER , USERNAME , PASSWORD );
132+ verifyStatic ();
133+ TmcUtil .tryToLogin (eq (ctx ), eq (expectedSettings ));
134+ }
95135}
0 commit comments