/** * Create a fake security token from a map of parameter strings, keys are one of: * ownerId, viewerId, domain, appUrl, appId, trustedJson, module * * @param paramMap * @return The fake token */ public static FakeGadgetToken createToken(Map<String, String> paramMap) { FakeGadgetToken fakeToken = new FakeGadgetToken(); fakeToken.setAppId(paramMap.get("appId")); fakeToken.setAppUrl(paramMap.get("appUrl")); fakeToken.setDomain(paramMap.get("domain")); fakeToken.setOwnerId(paramMap.get("ownerId")); fakeToken.setTrustedJson(paramMap.get("trustedJson")); fakeToken.setViewerId(paramMap.get("viewerId")); String moduleIdStr = paramMap.get("module"); if (moduleIdStr != null) { fakeToken.setModuleId(Integer.parseInt(moduleIdStr)); } return fakeToken; }
public FakeGadgetToken(String appId, String appUrl, String domain, String ownerId, String trustedJson, String viewerId, String moduleId) { setAppId(appId); setAppUrl(appUrl); setDomain(domain); setOwnerId(ownerId); setTrustedJson(trustedJson); setViewerId(viewerId); if (moduleId != null) { setModuleId(Long.parseLong(moduleId)); } }
@Before public void setUp() throws Exception { FAKE_TOKEN.setAppId("12345"); FAKE_TOKEN.setOwnerId("someowner"); FAKE_TOKEN.setViewerId("someowner"); converter = new BeanJsonConverter(Guice.createInjector()); request = new SocialRequestItem( Maps.<String, String[]>newHashMap(), FAKE_TOKEN, converter, converter); }
@Before public void setUp() throws Exception { injector = Guice.createInjector(); converter = new BeanJsonConverter(injector); authContext = new FakeGadgetToken(); featureRegistryProvider = new FeatureRegistryProvider() { public FeatureRegistry get(String repository) throws GadgetException { return mockRegistry; } }; authContext.setAppUrl("http://www.example.com/gadget.xml"); }
@Before public void setUp() { cacheProvider = new LruCacheProvider(100); cache = new DefaultHttpCache(cacheProvider); service = new DefaultInvalidationService(cache, cacheProvider, new AtomicLong()); appxToken = new FakeGadgetToken(); appxToken.setAppId("AppX"); appxToken.setOwnerId("OwnerX"); appxToken.setViewerId("ViewerX"); appyToken = new FakeGadgetToken(); appyToken.setAppId("AppY"); appyToken.setOwnerId("OwnerY"); appyToken.setViewerId("ViewerY"); signedRequest = new HttpRequest(URI); signedRequest.setAuthType(AuthType.SIGNED); signedRequest.setSecurityToken(appxToken); signedRequest.setOAuthArguments(new OAuthArguments()); signedRequest.getOAuthArguments().setUseToken(OAuthArguments.UseToken.NEVER); signedRequest.getOAuthArguments().setSignOwner(true); signedRequest.getOAuthArguments().setSignViewer(true); fetcher = new DefaultRequestPipelineTest.FakeHttpFetcher(); oauth = new DefaultRequestPipelineTest.FakeOAuthRequestProvider(); requestPipeline = new DefaultRequestPipeline(fetcher, cache, oauth, oauth2, new DefaultResponseRewriterRegistry(null, null), service, new HttpResponseMetadataHelper()); }
@Before public void setUp() throws Exception { backingStore = new BasicOAuthStore(); backingStore.setDefaultKey(new BasicOAuthStoreConsumerKeyAndSecret("key", "secret", KeyType.RSA_PRIVATE, "keyname", null)); backingStore.setDefaultCallbackUrl(DEFAULT_CALLBACK); store = new GadgetOAuthTokenStore(backingStore, new FakeGadgetSpecFactory()); socialToken = new FakeGadgetToken(); socialToken.setOwnerId("owner"); socialToken.setViewerId("viewer"); socialToken.setAppUrl(GADGET_URL); privateToken = new FakeGadgetToken(); privateToken.setOwnerId("owner"); privateToken.setViewerId("owner"); privateToken.setAppUrl(GADGET_URL); stateCrypter = new BasicBlobCrypter("abcdefghijklmnop".getBytes()); clientState = new OAuthClientState(stateCrypter); responseParams = new OAuthResponseParams(socialToken, null, stateCrypter); fetcherConfig = new OAuthFetcherConfig(stateCrypter, store, new FakeTimeSource(), null, false); }
@Override public SecurityToken getToken() { return new FakeGadgetToken(); }
@Test public void testChangeSecurityToken() throws Exception { // Doesn't actually sign since it returns the standard fetcher. // Signing tests are in SigningFetcherTest expectGetAndReturnBody(AuthType.SIGNED, RESPONSE_BODY); FakeGadgetToken authToken = new FakeGadgetToken() .setUpdatedToken("updated") .setAppUrl(DUMMY_TOKEN.getAppUrl()) .setContainer(DUMMY_TOKEN.getContainer()); expect(request.getAttribute(AuthInfoUtil.Attribute.SECURITY_TOKEN.getId())) .andReturn(authToken).atLeastOnce(); expect(request.getParameter(MakeRequestHandler.AUTHZ_PARAM)) .andReturn(AuthType.SIGNED.toString()).atLeastOnce(); expectParameters(request, MakeRequestHandler.AUTHZ_PARAM); replay(); handler.fetch(request, recorder); JSONObject results = extractJsonFromResponse(); assertEquals(RESPONSE_BODY, results.get("body")); assertEquals("updated", results.getString("st")); assertTrue(rewriter.responseWasRewritten()); }
@Test public void testGetUserId() throws Exception { UserId owner = new UserId(UserId.Type.owner, "hello"); assertEquals("owner", owner.getUserId(new FakeGadgetToken().setOwnerId("owner"))); UserId viewer = new UserId(UserId.Type.viewer, "hello"); assertEquals("viewer", viewer.getUserId(new FakeGadgetToken().setViewerId("viewer"))); UserId me = new UserId(UserId.Type.me, "hello"); assertEquals("viewer", me.getUserId(new FakeGadgetToken().setViewerId("viewer"))); UserId user = new UserId(UserId.Type.userId, "hello"); assertEquals("hello", user.getUserId(new FakeGadgetToken())); }
@Before public void setUp() throws Exception { token = new FakeGadgetToken(); token.setAppId("appId"); token.setViewerId("userX"); converter = mock(BeanJsonConverter.class); invalidationService = mock(InvalidationService.class); handler = new InvalidationHandler(invalidationService); registry = new DefaultHandlerRegistry(null, converter, new HandlerExecutionListener.NoOpHandler()); registry.addHandlers(Sets.<Object>newHashSet(handler)); params = Maps.newHashMap(); }
@Test public void testGetAndSetAndRemoveToken() { FakeGadgetToken t = new FakeGadgetToken(); ConsumerInfo consumer = new ConsumerInfo(null, null, null); t.setAppUrl("http://localhost:8080/samplecontainer/examples/oauth.xml"); t.setViewerId("viewer-one"); assertNull(store.getTokenInfo(t, consumer, "", "")); TokenInfo info = new TokenInfo("token", "secret", null, 0); store.setTokenInfo(t, consumer, "service", "token", info); info = store.getTokenInfo(t, consumer, "service", "token"); assertEquals("token", info.getAccessToken()); assertEquals("secret", info.getTokenSecret()); FakeGadgetToken t2 = new FakeGadgetToken(); t2.setAppUrl("http://localhost:8080/samplecontainer/examples/oauth.xml"); t2.setViewerId("viewer-two"); assertNull(store.getTokenInfo(t2, consumer, "service", "token")); store.removeToken(t, consumer, "service", "token"); assertNull(store.getTokenInfo(t, consumer, "service", "token")); }
@Before public void setUp() throws Exception { token = new FakeGadgetToken(); token.setAppId("appId"); converter = mock(BeanJsonConverter.class); activityService = mock(ActivityService.class); JSONObject config = new JSONObject('{' + ContainerConfig.DEFAULT_CONTAINER + ':' + "{'gadgets.features':{opensocial:" + "{supportedFields: {activity: ['id', 'title']}}" + "}}}"); containerConfig = new JsonContainerConfig(config, Expressions.forTesting()); handler = new ActivityHandler(activityService, containerConfig); registry = new DefaultHandlerRegistry(null, converter, new HandlerExecutionListener.NoOpHandler()); registry.addHandlers(ImmutableSet.<Object>of(handler)); }
@Test public void testFailInvalidateNoApp() throws Exception { String path = "/cache/invalidate"; RestHandler operation = registry.getRestHandler(path, "POST"); params.put(InvalidationHandler.KEYS_PARAM, new String[]{"http://www.example.org/gadget.xml"}); try { token.setAppId(""); token.setAppUrl(""); operation.execute(params, null, token, converter).get(); fail("Expected error"); } catch (ExecutionException ee) { assertTrue(ee.getCause() instanceof ProtocolException); } } }
@Test public void testGetOAuthAccessor_oauthParamsInBody() throws Exception { OAuthArguments arguments = new OAuthArguments(); arguments.setServiceName("testservice"); arguments.setUseToken(UseToken.ALWAYS); privateToken.setAppUrl("http://www.example.com/body.xml"); AccessorInfo info = store.getOAuthAccessor(privateToken, arguments, clientState, responseParams, fetcherConfig); assertEquals( FakeOAuthServiceProvider.REQUEST_TOKEN_URL, info.getConsumer().getConsumer().serviceProvider.requestTokenURL); assertEquals( FakeOAuthServiceProvider.APPROVAL_URL, info.getConsumer().getConsumer().serviceProvider.userAuthorizationURL); assertEquals( FakeOAuthServiceProvider.ACCESS_TOKEN_URL, info.getConsumer().getConsumer().serviceProvider.accessTokenURL); assertEquals(HttpMethod.POST, info.getHttpMethod()); assertEquals(OAuthParamLocation.POST_BODY, info.getParamLocation()); }
@Before public void setUp() throws Exception { expect(request.getMethod()).andReturn("POST").anyTimes(); expect(request.getParameter(Param.URL.getKey())) .andReturn(REQUEST_URL.toString()).anyTimes(); JSONObject config = new JSONObject('{' + ContainerConfig.DEFAULT_CONTAINER + ':' + "{'gadgets.container': ['default']," + "'gadgets.features':{views:" + "{aliased: {aliases: ['some-alias', 'alias']}}" + ",'core.io':" + "{unparseableCruft :\"throw 1; < don't be evil' >\"}}}}"); containerConfig = new JsonContainerConfig(config, Expressions.forTesting()); ldService = new HashLockedDomainService(containerConfig, false, new HashShaLockedDomainPrefixGenerator()); handler = new MakeRequestHandler(containerConfig, pipeline, rewriterRegistry, feedProcessorProvider, gadgetAdminStore, processor, ldService); DUMMY_TOKEN.setAppUrl("http://some/gadget.xml"); DUMMY_TOKEN.setContainer(ContainerConfig.DEFAULT_CONTAINER); expect(request.getParameter(Param.GADGET.getKey())).andReturn("http://some/gadget.xml").anyTimes(); expect(processor.process(capture(context))).andReturn(gadget).anyTimes(); expect(gadgetAdminStore.isWhitelisted(isA(String.class), isA(String.class))).andReturn(true); }
@Test public void testGetAppId() throws Exception { request.setParameter("appId", "100"); assertEquals("100", request.getAppId()); request.setParameter("appId", "@app"); assertEquals(FAKE_TOKEN.getAppId(), request.getAppId()); }
@Before public void setUp() throws Exception { FAKE_TOKEN.setAppId("12345"); FAKE_TOKEN.setOwnerId("someowner"); FAKE_TOKEN.setViewerId("someowner"); converter = new BeanJsonConverter(Guice.createInjector()); request = new BaseRequestItem( Maps.<String,String[]>newHashMap(), FAKE_TOKEN, converter, converter); }
@Before public void setUp() { cacheProvider = new LruCacheProvider(100); cache = new DefaultHttpCache(cacheProvider); service = new DefaultInvalidationService(cache, cacheProvider, new AtomicLong()); appxToken = new FakeGadgetToken(); appxToken.setAppId("AppX"); appxToken.setOwnerId("OwnerX"); appxToken.setViewerId("ViewerX"); appyToken = new FakeGadgetToken(); appyToken.setAppId("AppY"); appyToken.setOwnerId("OwnerY"); appyToken.setViewerId("ViewerY"); signedRequest = new HttpRequest(URI); signedRequest.setAuthType(AuthType.SIGNED); signedRequest.setSecurityToken(appxToken); signedRequest.setOAuthArguments(new OAuthArguments()); signedRequest.getOAuthArguments().setUseToken(OAuthArguments.UseToken.NEVER); signedRequest.getOAuthArguments().setSignOwner(true); signedRequest.getOAuthArguments().setSignViewer(true); fetcher = new DefaultRequestPipelineTest.FakeHttpFetcher(); oauth = new DefaultRequestPipelineTest.FakeOAuthRequestProvider(); requestPipeline = new DefaultRequestPipeline(fetcher, cache, oauth, new DefaultResponseRewriterRegistry(null, null), service, new HttpResponseMetadataHelper()); }
@Before public void setUp() throws Exception { backingStore = new BasicOAuthStore(); backingStore.setDefaultKey(new BasicOAuthStoreConsumerKeyAndSecret("key", "secret", KeyType.RSA_PRIVATE, "keyname", null)); backingStore.setDefaultCallbackUrl(DEFAULT_CALLBACK); store = new GadgetOAuthTokenStore(backingStore, new FakeGadgetSpecFactory()); socialToken = new FakeGadgetToken(); socialToken.setOwnerId("owner"); socialToken.setViewerId("viewer"); socialToken.setAppUrl(GADGET_URL); privateToken = new FakeGadgetToken(); privateToken.setOwnerId("owner"); privateToken.setViewerId("owner"); privateToken.setAppUrl(GADGET_URL); stateCrypter = new BasicBlobCrypter("abcdefghijklmnop".getBytes()); clientState = new OAuthClientState(stateCrypter); responseParams = new OAuthResponseParams(socialToken, null, stateCrypter); fetcherConfig = new OAuthFetcherConfig(stateCrypter, store, new FakeTimeSource(), null, false); }
@Before public void setUp() throws Exception { injector = Guice.createInjector(); converter = new BeanJsonConverter(injector); token = new FakeGadgetToken(); token.setAppUrl("http://www.example.com/gadget.xml"); }