@@ -2,6 +2,9 @@ package create
22
33import (
44 "context"
5+ "encoding/base64"
6+ "encoding/json"
7+ "fmt"
58 "testing"
69
710 "github.com/google/go-cmp/cmp"
@@ -25,6 +28,15 @@ var testParentId = uuid.NewString()
2528var testNetworkAreaId = uuid .NewString ()
2629var testEmail = "email"
2730
31+ // buildTestJWT creates an unsigned JWT token containing the given email claim.
32+ // getEmailFromToken uses ParseUnverified, so no real signing key is needed.
33+ func buildTestJWT (email string ) string {
34+ header , _ := json .Marshal (map [string ]string {"alg" : "HS256" , "typ" : "JWT" })
35+ payload , _ := json .Marshal (map [string ]string {"email" : email })
36+ enc := base64 .RawURLEncoding
37+ return fmt .Sprintf ("%s.%s.fakesig" , enc .EncodeToString (header ), enc .EncodeToString (payload ))
38+ }
39+
2840func fixtureFlagValues (mods ... func (flagValues map [string ]string )) map [string ]string {
2941 flagValues := map [string ]string {
3042 parentIdFlag : testParentId ,
@@ -193,6 +205,7 @@ func TestBuildRequest(t *testing.T) {
193205 authFlow auth.AuthFlow
194206 sa_email * string
195207 user_email * string
208+ accessToken * string
196209 expectedRequest resourcemanager.ApiCreateProjectRequest
197210 isValid bool
198211 }{
@@ -220,6 +233,13 @@ func TestBuildRequest(t *testing.T) {
220233 expectedRequest : fixtureRequest (),
221234 isValid : true ,
222235 },
236+ {
237+ description : "access_token_env_var_no_stored_auth_flow" ,
238+ model : fixtureInputModel (),
239+ accessToken : utils .Ptr (buildTestJWT (testEmail )),
240+ expectedRequest : fixtureRequest (),
241+ isValid : true ,
242+ },
223243 {
224244 description : "missing_network_area_id sa_key" ,
225245 model : fixtureInputModel (
@@ -296,6 +316,9 @@ func TestBuildRequest(t *testing.T) {
296316 t .Fatalf ("Failed to set user email in storage: %v" , err )
297317 }
298318 }
319+ if tt .accessToken != nil {
320+ t .Setenv ("STACKIT_ACCESS_TOKEN" , * tt .accessToken )
321+ }
299322 request , err := buildRequest (testCtx , tt .model , testClient )
300323 if err != nil {
301324 if ! tt .isValid {
0 commit comments