Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. @ManagedBean(name = "authBean")
  2. @SessionScoped
  3. public class AuthorizationBean implements Serializable{
  4.  
  5. //Data access object for the users
  6. @Inject
  7. UserDao userDao;
  8.  
  9. private User user; // The JPA entity.
  10. public User getUser() {
  11. if (user == null) {
  12. user = (User) FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
  13. if (user == null) {
  14. Principal principal = FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
  15. if (principal != null) {
  16. user = userDao.findByEmail(principal.getName()); // Find User by j_username.
  17. }
  18. }
  19. }
  20. return user;
  21. }
  22.  
  23. /**
  24. * Function that handles the logout
  25. * @return Redirect string that points to the login page
  26. */
  27. public String doLogout() {
  28. // invalidate the session, so that the session is removed
  29. FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
  30. user = null;
  31. // return redirect to login page
  32. return "/login.xhtml?faces-redirect=true";
  33. }
  34.  
  35.  
  36. }
  37.  
  38. @RunWith(PowerMockRunner.class)
  39. @PrepareForTest(FacesContext.class)
  40. public class AuthorizationBeanTest {
  41.  
  42. private AuthorizationBean authorizationBean;
  43.  
  44. @Mock
  45. User user;
  46.  
  47. @Mock
  48. FacesContext facesContext;
  49.  
  50. @Mock
  51. ExternalContext externalContext;
  52.  
  53. @Before
  54. public void setUp() {
  55. authorizationBean = new AuthorizationBean();
  56. Map<String,Object> sessionMap = new HashMap<>();
  57. sessionMap.put("user", user);
  58.  
  59. //Mocking the static function getCurrentInstance from FacesContext,
  60. //so a mocked user can be returned for the test
  61. PowerMockito.mockStatic(FacesContext.class);
  62. PowerMockito.when(FacesContext.getCurrentInstance()).thenReturn(facesContext);
  63. when(facesContext.getExternalContext()).thenReturn(externalContext);
  64. when(externalContext.getSessionMap()).thenReturn(sessionMap);
  65. }
  66.  
  67. @Test
  68. public void doLogoutTest() throws NoSuchFieldException, IllegalAccessException {
  69. assertNotNull(authorizationBean.getUser());
  70. assertEquals("/login.xhtml?faces-redirect=true", authorizationBean.doLogout());
  71.  
  72. //Check through reflection if the private field user is now null
  73. Field userField = authorizationBean.getClass().getDeclaredField("user");
  74. userField.setAccessible(true);
  75. User testUser = (User) userField.get(authorizationBean);
  76. assertNull(testUser);
  77. }
  78.  
  79. }
  80.  
  81. protected boolean isUserNull () {
  82. return user==null;
  83. }
  84.  
  85. //Mocking the static function getCurrentInstance from FacesContext,
  86. //so a mocked user can be returned for the test
  87. PowerMockito.mockStatic(FacesContext.class);
  88. PowerMockito.when(FacesContext.getCurrentInstance()).thenReturn(facesContext);
  89. when(facesContext.getExternalContext()).thenReturn(externalContext);
  90. when(externalContext.getSessionMap()).thenReturn(sessionMap);
  91.  
  92. FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("user");
  93.  
  94. FacesContext.getCurrentInstance().getExternalContext().getUserPrincipal();
  95.  
  96. userDao.findByEmail(principal.getName());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement