Skip to content
Snippets Groups Projects
rootmux_test.go 6.71 KiB
Newer Older
  • Learn to ignore specific revisions
  • package rootmux
    
    import (
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    	"encoding/base64"
    
    	"net/http/cookiejar"
    	"net/http/httptest"
    	"net/url"
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    	"os"
    	"regexp"
    	"strings"
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    	"forge.grandlyon.com/npernoud/glcpro/internal/apientreprise"
    	"forge.grandlyon.com/npernoud/glcpro/internal/franceconnect"
    
    	"forge.grandlyon.com/npernoud/glcpro/internal/matcher"
    
    	"forge.grandlyon.com/npernoud/glcpro/pkg/tester"
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    	"forge.grandlyon.com/npernoud/glcpro/pkg/tokens"
    
    	noH               map[string]string
    	fcServer          *httptest.Server
    	clientRedirectURI = "http://client.org"
    	tokenMustContent  = `"id_token":{"given_name":"Angela`
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    func TestMain(m *testing.M) {
    	// Create the france connect mock server
    
    	fcServer = httptest.NewServer(franceconnect.CreateMock())
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    	defer fcServer.Close()
    	// Setup to use the france connect mock server
    	os.Setenv("FC_AUTH", fcServer.URL+"/auth")
    	os.Setenv("FC_TOKEN", fcServer.URL+"/token")
    	os.Setenv("FC_USER_INFO", fcServer.URL+"/userinfo")
    	franceconnect.Init()
    	// Setup the token manager to use debug mode
    	os.Setenv("DEBUG_MODE", "true")
    	tokens.Init()
    	// Setup the api entreprise handler to use debug mode
    	apientreprise.Init("../../configs/apicache.json")
    
    	code := m.Run()
    
    	// Remove the database
    	os.Remove("./glcpro.db")
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    	os.Exit(code)
    }
    
    
    func createTester(t *testing.T) (*httptest.Server, tester.DoFn, tester.DoFn) {
    	// Create the server
    	mux := CreateRootMux("web")
    	ts := httptest.NewServer(mux)
    	url, _ := url.Parse(ts.URL)
    	port := url.Port()
    	// Create the cookie jar
    	jar, _ := cookiejar.New(nil)
    	// wrap the testing function
    	return ts, tester.CreateServerTester(t, port, jar), tester.CreateServerTester(t, port, nil)
    }
    
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    // Test the use case 1 : "Cas d'usage 1 : Accès à une démarche depuis un portail de service public pour un dirigeant"
    func TestUseCase1(t *testing.T) {
    
    
    	// Create the tester
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    	_, do, _ := createTester(t)
    
    	// (We arrive from a client with a query, and the front end add the requested SIRENT to the query), we should be redirected to france connect, login, and be back with an authorisation code
    	r := do("GET", "/api/oidc/auth?scope=openid%20profile&client_id=A_RANDOM_ID&redirect_uri="+clientRedirectURI+"&response_type=code&state=A_RANDOM_STATE&sirent=000000001", noH, "", 302, "")
    
    	// We are redirected to France Connect
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    	r = redirectURIFromBody(r)
    
    	fmt.Printf("Redirected to France Connect : %v\n", r)
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    	r = do("GET", r, noH, "", 302, "")
    
    	// We are redirected to GLC Pro (France Connect callback)
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    	r = redirectURIFromBody(r)
    
    	fmt.Printf("Redirected to GLC Pro callback : %v\n", r)
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    	r = do("GET", r, noH, "", 302, "")
    
    	// We are redirected to the client
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    	r = redirectURIFromBody(r)
    
    	fmt.Printf("Redirected to Client : %v\n", r)
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    	if !strings.Contains(r, clientRedirectURI) {
    		t.Errorf("no redirection to the client")
    	}
    	code := regexp.MustCompile(`.*code=(.*)`).FindStringSubmatch(r)[1]
    	// We now use the code to get the token
    	tk := do("POST", "/api/oidc/token", noH, "client_id=A_RANDOM_ID&client_secret=A_RANDOM_SECRET&grant_type=authorization_code&code="+code, 200, "")
    	tk = regexp.MustCompile(`id_token=(.*)&scope.*`).FindStringSubmatch(tk)[1]
    	token, _ := base64.StdEncoding.DecodeString(tk)
    
    	if !strings.Contains(string(token), tokenMustContent) || !strings.Contains(string(token), "THE TEST COMPANY") {
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    		t.Errorf("id token is not complete")
    
    	fmt.Printf("Token : %v\n", string(token))
    }
    
    // Test the use case 2 : "Cas d'usage 2 : Demande d'habilitation par un dirigeant"
    func TestUseCase2(t *testing.T) {
    
    	// Create the tester
    	_, do, _ := createTester(t)
    
    	// Create the mock mail server
    	recorder := matcher.SetTestModeAndReturnRecorder()
    
    	/////////////////////////////////////
    	//  THE MANDATEE MAKES THE DEMAND  //
    	/////////////////////////////////////
    
    	// (We arrive from a client with a query, and the front end add the requested SIRENT to the query), we should be redirected to france connect, login, .. and do NOT get a code but be redirect on the mandate demand page
    	// Configure FC Mock to give the ID of Paul Louis Dupont
    	os.Setenv("FC_USER_INFO", fcServer.URL+"/userinfo2")
    	franceconnect.Init()
    	r := do("GET", "/api/oidc/auth?scope=openid%20profile&client_id=A_RANDOM_ID&redirect_uri="+clientRedirectURI+"&response_type=code&state=A_RANDOM_STATE&sirent=000000001", noH, "", 302, "")
    	// We are redirected to France Connect
    	r = redirectURIFromBody(r)
    	fmt.Printf("Redirected to France Connect : %v\n", r)
    	r = do("GET", r, noH, "", 302, "")
    	// We are redirected to GLC Pro (France Connect callback)
    	r = redirectURIFromBody(r)
    	fmt.Printf("Redirected to GLC Pro callback : %v\n", r)
    	r = do("GET", r, noH, "", 302, "")
    	// We are redirected to the matcher
    	r = redirectURIFromBody(r)
    	fmt.Printf("Redirected to GLC Pro matcher : %v\n", r)
    	// We are redirected to the matcher
    	if !strings.Contains(r, "/matcher") {
    		t.Errorf("no redirection to the matcher")
    	}
    	// Let's send a mail to the company CEO
    	do("POST", "/api/matcher/demand", noH, "angela@testcompany.com", 200, "")
    	fmt.Printf("Sent mail : %v\n", recorder.Msg())
    	if !strings.Contains(recorder.Msg(), "Demande de mandatement") {
    		t.Errorf("received body is not what is expected")
    	}
    
    	////////////////////////////////////////////
    	//  THE COMPANY CEO VALIDATES THE DEMAND  //
    	////////////////////////////////////////////
    
    	// Configure FC Mock to give the ID of Angela Claire Louise DUBOIS, configure the FC Auth to callback to the matcher (what is normaly made by the js front client)
    	os.Setenv("FC_USER_INFO", fcServer.URL+"/userinfo")
    	os.Setenv("FC_AUTH", fcServer.URL+"/auth2")
    	franceconnect.Init()
    	// Create a new tester with a new cookie jar (because we are someone else, on a different computer)
    	_, do, _ = createTester(t)
    	// Extract the link from the mail
    	r = do("GET", "/api/matcher/validate?code="+regexp.MustCompile(`code=(.*)"`).FindStringSubmatch(recorder.Msg())[1], noH, "", 302, "")
    	// We are redirected to France Connect
    	r = redirectURIFromBody(r)
    	fmt.Printf("Redirected to France Connect : %v\n", r)
    	r = do("GET", r, noH, "", 302, "")
    	fmt.Printf("Body 5 : %v\n", r)
    	r = redirectURIFromBody(r)
    	r = do("GET", r, noH, "", 302, "")
    	fmt.Printf("Body 6 : %v\n", r)
    	r = redirectURIFromBody(r)
    	if r != "/mandatecreated" {
    		t.Errorf("CEO was not redirected to the mandate created information")
    	}
    
    	///////////////////////////////////////////////////
    	//  THE MANDATEE USE THE NEWLY OBTAINED MANDATE  //
    	///////////////////////////////////////////////////
    
    	// It's actually the use case 1, only with France Connect mock giving the identity of Paul Louis Dubois
    	os.Setenv("FC_USER_INFO", fcServer.URL+"/userinfo2")
    	os.Setenv("FC_AUTH", fcServer.URL+"/auth")
    	franceconnect.Init()
    	tokenMustContent = `"id_token":{"given_name":"Paul`
    	TestUseCase1(t)
    
    Nicolas Pernoud's avatar
    Nicolas Pernoud committed
    func redirectURIFromBody(body string) string {
    	return regexp.MustCompile(`<a href="(.*)">Found</a>`).FindStringSubmatch(body)[1]