-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ : add options in login page to choose oauth2 connections
- Loading branch information
1 parent
0cbcd07
commit 833f63b
Showing
6 changed files
with
163 additions
and
19 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
src/main/java/io/codeka/gaia/login/controller/LoginController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.codeka.gaia.login.controller; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties; | ||
import org.springframework.stereotype.Controller; | ||
import org.springframework.ui.Model; | ||
import org.springframework.web.bind.annotation.GetMapping; | ||
|
||
@Controller | ||
public class LoginController { | ||
|
||
private OAuth2ClientProperties oAuth2ClientProperties; | ||
|
||
@Autowired(required = false) | ||
public void setOAuth2ClientProperties(OAuth2ClientProperties oAuth2ClientProperties) { | ||
this.oAuth2ClientProperties = oAuth2ClientProperties; | ||
} | ||
|
||
@GetMapping("/login") | ||
public String login(Model model) { | ||
if (oAuth2ClientProperties != null) { | ||
model.addAttribute("clients", oAuth2ClientProperties.getRegistration().keySet()); | ||
} | ||
return "login"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/test/java/io/codeka/gaia/login/controller/LoginControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package io.codeka.gaia.login.controller; | ||
|
||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties; | ||
import org.springframework.ui.Model; | ||
|
||
import java.util.HashMap; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.Mockito.*; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
class LoginControllerTest { | ||
|
||
private LoginController controller; | ||
|
||
@Mock | ||
OAuth2ClientProperties oAuth2ClientProperties; | ||
|
||
@Mock | ||
Model model; | ||
|
||
@BeforeEach | ||
void setup() { | ||
controller = new LoginController(); | ||
controller.setOAuth2ClientProperties(oAuth2ClientProperties); | ||
} | ||
|
||
@Test | ||
void login_shouldAddOAuth2ClientsInModel_whenConfigured() { | ||
// given | ||
var oAuth2clients = new HashMap<String, OAuth2ClientProperties.Registration>(); | ||
oAuth2clients.put("client_1", new OAuth2ClientProperties.Registration()); | ||
oAuth2clients.put("client_2", new OAuth2ClientProperties.Registration()); | ||
oAuth2clients.put("client_3", new OAuth2ClientProperties.Registration()); | ||
|
||
// when | ||
when(oAuth2ClientProperties.getRegistration()).thenReturn(oAuth2clients); | ||
controller.login(model); | ||
|
||
// then | ||
verify(model).addAttribute("clients", oAuth2clients.keySet()); | ||
} | ||
|
||
@Test | ||
void login_shouldIgnoreOAuth2_whenNotConfigured() { | ||
// when | ||
controller.setOAuth2ClientProperties(null); | ||
controller.login(model); | ||
|
||
// then | ||
verify(model, never()).addAttribute(eq("clients"), any()); | ||
} | ||
|
||
@Test | ||
void login_shouldReturnTheCorrectView() { | ||
// when | ||
var result = controller.login(model); | ||
|
||
// then | ||
assertEquals("login", result); | ||
} | ||
} |