/**
 * © David Attias 2015
 */
package io.spacedog.test.credentials;

import org.junit.Test;

import io.spacedog.client.SpaceDog;
import io.spacedog.client.credentials.CredentialsSettings;
import io.spacedog.client.credentials.CredentialsSettings.OAuthSettings;
import io.spacedog.client.http.SpaceEnv;
import io.spacedog.client.http.SpaceRequest;
import io.spacedog.test.SpaceTest;

public class LinkedinRestyTest extends SpaceTest {

	@Test
	public void login() {

		// prepare
		prepareTest();
		SpaceEnv env = SpaceEnv.env();
		SpaceDog superadmin = clearServer();
		String redirectUri = superadmin.backend().url("/1/login/linkedin");

		// no linkedin settings means no linkedin credentials
		SpaceRequest.post("/1/login/linkedin")//
				.backend(superadmin.backend())//
				.formField("code", "XXX")//
				.formField("redirect_uri", "XXX")//
				.go(400);

		// admin sets linkedin settings without redirect uri
		CredentialsSettings settings = new CredentialsSettings();
		settings.linkedin = new OAuthSettings();
		settings.linkedin.backendUrl = "https://www.linkedin.com";
		settings.linkedin.clientId = env.getOrElseThrow("spacedog.test.linkedin.client.id");
		settings.linkedin.clientSecret = env.getOrElseThrow("spacedog.test.linkedin.client.secret");
		superadmin.credentials().settings(settings);

		// fails to create linkedin credentials if no authorization code
		SpaceRequest.post("/1/login/linkedin")//
				.backend(superadmin.backend())//
				.formField("redirect_uri", redirectUri)//
				.go(400);

		// fails to create linkedin credentials if invalid redirect_uri
		SpaceRequest.post("/1/login/linkedin")//
				.backend(superadmin.backend())//
				.formField("code", "XXX")//
				.formField("redirect_uri", "XXX")//
				.go(400);

		// fails to create linkedin credentials if invalid code
		SpaceRequest.post("/1/login/linkedin")//
				.backend(superadmin.backend())//
				.formField("code", "XXX")//
				.formField("redirect_uri", redirectUri)//
				.go(400);

		// fails to create linkedin credentials if invalid code
		// if no redirect_uri parameter is set spacedog uses
		// its default redirect uri
		SpaceRequest.post("/1/login/linkedin")//
				.backend(superadmin.backend())//
				.formField("code", "XXX")//
				.go(400);
	}
}

@@ -1733,33 +1733,33 @@
 X%22)//%0A%09%09%09%09.go(40
-0
+1
 );%0A%0A%09%09// fails t
@@ -1964,33 +1964,33 @@
 ri)//%0A%09%09%09%09.go(40
-0
+1
 );%0A%0A%09%09// fails t
@@ -2242,17 +2242,17 @@
 %09%09.go(40
-0
+1
 );%0A%09%7D%0A%7D%0A

Fix linkedin resty test: linkedin now returns 401 if invalid code