Newer
Older
"net/http/cookiejar"
"net/http/httptest"
"net/url"
"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"
noH map[string]string
fcServer *httptest.Server
clientRedirectURI = "http://client.org"
tokenMustContent = `"id_token":{"given_name":"Angela`
func TestMain(m *testing.M) {
// Create the france connect mock server
fcServer = httptest.NewServer(franceconnect.CreateMock())
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")
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)
}
// 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) {
_, 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
fmt.Printf("Redirected to France Connect : %v\n", r)
// We are redirected to GLC Pro (France Connect callback)
fmt.Printf("Redirected to GLC Pro callback : %v\n", r)
fmt.Printf("Redirected to Client : %v\n", r)
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") {
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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)
func redirectURIFromBody(body string) string {
return regexp.MustCompile(`<a href="(.*)">Found</a>`).FindStringSubmatch(body)[1]