@Test public void failedLoginTest() throws Exception { final Context contextMocked = mock(Context.class); final Request requestMocked = mock(Request.class); final Payload payloadMocked = mock(Payload.class); final TempBlob tempBlob = mock(TempBlob.class); final String someJson = "{\"name\":\"name\",\"password\":\"password\"}"; when(contextMocked.getRequest()).thenReturn(requestMocked); when(requestMocked.getPayload()).thenReturn(payloadMocked); when(storageFacet.createTempBlob(eq(payloadMocked), any())).thenReturn(tempBlob); when(tempBlob.get()).thenReturn(new ByteArrayInputStream(someJson.getBytes(StandardCharsets.UTF_8))); when(npmTokenManager.login("name", "password")).thenReturn(null); Response response = underTest.login(contextMocked); assertThat("login call should have returned a value", response, notNullValue() ); verify(npmTokenManager).login("name", "password"); assertThat("expecting a 401 for the status", response.getStatus().getCode(), is(401)); }
@Override public Response login(final Context context) { final Payload payload = context.getRequest().getPayload(); if (payload == null) { return NpmResponses.badRequest("Missing body"); } StorageFacet storageFacet = facet(StorageFacet.class); try (TempBlob tempBlob = storageFacet.createTempBlob(payload, NpmFacetUtils.HASH_ALGORITHMS)) { NestedAttributesMap request = NpmJsonUtils.parse(tempBlob); String token = npmTokenManager.login(request.get("name", String.class), request.get("password", String.class)); if (null != token) { NestedAttributesMap response = new NestedAttributesMap("response", Maps.newHashMap()); response.set("ok", Boolean.TRUE.toString()); response.set("rev", "_we_dont_use_revs_any_more"); response.set("id", "org.couchdb.user:undefined"); response.set("token", token); return HttpResponses.created(new BytesPayload(NpmJsonUtils.bytes(response), ContentTypes.APPLICATION_JSON)); } else { return NpmResponses.badCredentials("Bad username or password"); } } catch (IOException e) { throw new RuntimeException(e); } }
NpmTokenFacetImpl extends FacetSupport implements NpmTokenFacet { @Override public Response login(final Context context) { final Payload payload = context.getRequest().getPayload(); if (payload == null) { return NpmResponses.badRequest("Missing body"); } StorageFacet storageFacet = facet(StorageFacet.class); try (TempBlob tempBlob = storageFacet.createTempBlob(payload, NpmFacetUtils.HASH_ALGORITHMS)) { NestedAttributesMap request = NpmJsonUtils.parse(tempBlob); String token = npmTokenManager.login(request.get("name", String.class), request.get("password", String.class)); if (null != token) { NestedAttributesMap response = new NestedAttributesMap("response", Maps.newHashMap()); response.set("ok", Boolean.TRUE.toString()); response.set("rev", "_we_dont_use_revs_any_more"); response.set("id", "org.couchdb.user:undefined"); response.set("token", token); return HttpResponses.created(new BytesPayload(NpmJsonUtils.bytes(response), ContentTypes.APPLICATION_JSON)); } else { return NpmResponses.badCredentials("Bad username or password"); } } catch (IOException e) { throw new RuntimeException(e); } } }
NpmTokenFacetImpl extends FacetSupport implements NpmTokenFacet { @Override public Response login(final Context context) { final Payload payload = context.getRequest().getPayload(); if (payload == null) { return NpmResponses.badRequest("Missing body"); } StorageFacet storageFacet = facet(StorageFacet.class); try (TempBlob tempBlob = storageFacet.createTempBlob(payload, NpmFacetUtils.HASH_ALGORITHMS)) { NestedAttributesMap request = NpmJsonUtils.parse(tempBlob); String token = npmTokenManager.login(request.get("name", String.class), request.get("password", String.class)); if (null != token) { NestedAttributesMap response = new NestedAttributesMap("response", Maps.newHashMap()); response.set("ok", Boolean.TRUE.toString()); response.set("rev", "_we_dont_use_revs_any_more"); response.set("id", "org.couchdb.user:undefined"); response.set("token", token); return HttpResponses.created(new BytesPayload(NpmJsonUtils.bytes(response), ContentTypes.APPLICATION_JSON)); } else { return NpmResponses.badCredentials("Bad username or password"); } } catch (IOException e) { throw new RuntimeException(e); } } @Inject  NpmTokenFacetImpl(final NpmTokenManager npmTokenManager); }
NpmTokenFacetImpl extends FacetSupport implements NpmTokenFacet { @Override public Response login(final Context context) { final Payload payload = context.getRequest().getPayload(); if (payload == null) { return NpmResponses.badRequest("Missing body"); } StorageFacet storageFacet = facet(StorageFacet.class); try (TempBlob tempBlob = storageFacet.createTempBlob(payload, NpmFacetUtils.HASH_ALGORITHMS)) { NestedAttributesMap request = NpmJsonUtils.parse(tempBlob); String token = npmTokenManager.login(request.get("name", String.class), request.get("password", String.class)); if (null != token) { NestedAttributesMap response = new NestedAttributesMap("response", Maps.newHashMap()); response.set("ok", Boolean.TRUE.toString()); response.set("rev", "_we_dont_use_revs_any_more"); response.set("id", "org.couchdb.user:undefined"); response.set("token", token); return HttpResponses.created(new BytesPayload(NpmJsonUtils.bytes(response), ContentTypes.APPLICATION_JSON)); } else { return NpmResponses.badCredentials("Bad username or password"); } } catch (IOException e) { throw new RuntimeException(e); } } @Inject  NpmTokenFacetImpl(final NpmTokenManager npmTokenManager); @Override Response login(final Context context); @Override Response logout(final Context context); }
NpmTokenFacetImpl extends FacetSupport implements NpmTokenFacet { @Override public Response login(final Context context) { final Payload payload = context.getRequest().getPayload(); if (payload == null) { return NpmResponses.badRequest("Missing body"); } StorageFacet storageFacet = facet(StorageFacet.class); try (TempBlob tempBlob = storageFacet.createTempBlob(payload, NpmFacetUtils.HASH_ALGORITHMS)) { NestedAttributesMap request = NpmJsonUtils.parse(tempBlob); String token = npmTokenManager.login(request.get("name", String.class), request.get("password", String.class)); if (null != token) { NestedAttributesMap response = new NestedAttributesMap("response", Maps.newHashMap()); response.set("ok", Boolean.TRUE.toString()); response.set("rev", "_we_dont_use_revs_any_more"); response.set("id", "org.couchdb.user:undefined"); response.set("token", token); return HttpResponses.created(new BytesPayload(NpmJsonUtils.bytes(response), ContentTypes.APPLICATION_JSON)); } else { return NpmResponses.badCredentials("Bad username or password"); } } catch (IOException e) { throw new RuntimeException(e); } } @Inject  NpmTokenFacetImpl(final NpmTokenManager npmTokenManager); @Override Response login(final Context context); @Override Response logout(final Context context);  }