NpmTokenManager extends BearerTokenManager { public String login(final String username, final String password) { checkNotNull(username); checkNotNull(password); try { AuthenticationInfo authenticationInfo = securityHelper.getSecurityManager().authenticate( new UsernamePasswordToken(username, password)); return super.createToken(authenticationInfo.getPrincipals()); } catch (AuthenticationException e) { log.debug("Bad credentials provided for npm token creation", e); return null; } } @Inject  NpmTokenManager(final ApiKeyStore apiKeyStore, final SecurityHelper securityHelper); String login(final String username, final String password); boolean logout();  }
@Test(expected = NullPointerException.class) public void loginFailFastWhenUsernameIsNull() throws Exception { underTest.login(null, "password"); }
@Test(expected = NullPointerException.class) public void loginFailFastWhenPasswordIsNull() throws Exception { underTest.login("username", null); }
@Test public void createTokenOnLogin() throws Exception { when(apiKeyStore.getApiKey(any(), any())).thenReturn(TOKEN.toCharArray()); assertThat(underTest.login("username", "password"), is(equalTo(NpmToken.NAME + "." + TOKEN))); }
@Test public void nullOnAuthException() throws Exception { when(apiKeyStore.getApiKey(any(), any())).thenThrow(new AuthenticationException()); assertThat(underTest.login("username", "password"), is(nullValue())); }