|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "net/http" |
| 6 | + "net/http/httptest" |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestAuthUserInfoProfileNormalizesCurrentGlobalShape(t *testing.T) { |
| 12 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 13 | + if r.URL.Path != "/api/v1/userinfo" { |
| 14 | + http.NotFound(w, r) |
| 15 | + return |
| 16 | + } |
| 17 | + w.Header().Set("Content-Type", "application/json") |
| 18 | + _, _ = w.Write([]byte(`{ |
| 19 | + "id":"synthetic-openapi-id", |
| 20 | + "name":"Synthetic Display", |
| 21 | + "nick_name":"Synthetic Nick", |
| 22 | + "username":"synthetic-login", |
| 23 | + "avatar":"https://qoder.example/avatar", |
| 24 | + "email":"synthetic@example.invalid", |
| 25 | + "organization_id":"synthetic-org", |
| 26 | + "organization_name":"Synthetic Org", |
| 27 | + "is_privacy_policy_modifiable":false |
| 28 | + }`)) |
| 29 | + })) |
| 30 | + defer server.Close() |
| 31 | + |
| 32 | + manager := &authManager{ |
| 33 | + openapiBase: server.URL, |
| 34 | + httpc: server.Client(), |
| 35 | + logf: func(string, ...any) {}, |
| 36 | + } |
| 37 | + profile, err := manager.requestUserInfo(context.Background(), "synthetic-token") |
| 38 | + if err != nil { |
| 39 | + t.Fatalf("requestUserInfo() error = %v", err) |
| 40 | + } |
| 41 | + if profile.UID != "synthetic-openapi-id" { |
| 42 | + t.Fatalf("profile UID = %q, want current-shape id", profile.UID) |
| 43 | + } |
| 44 | + if profile.Name != "Synthetic Display" { |
| 45 | + t.Fatalf("profile Name = %q, want name", profile.Name) |
| 46 | + } |
| 47 | + if profile.OrgID != "synthetic-org" || profile.OrgName != "Synthetic Org" { |
| 48 | + t.Fatalf("profile organization = %q/%q, want synthetic-org/Synthetic Org", profile.OrgID, profile.OrgName) |
| 49 | + } |
| 50 | + if profile.AvatarURL != "https://qoder.example/avatar" { |
| 51 | + t.Fatalf("profile AvatarURL = %q, want avatar fallback", profile.AvatarURL) |
| 52 | + } |
| 53 | + if profile.IsDataPolicyModifiable == nil || *profile.IsDataPolicyModifiable { |
| 54 | + t.Fatal("profile privacy-policy flag was not normalized to false") |
| 55 | + } |
| 56 | + |
| 57 | + ui := &userInfo{} |
| 58 | + applyAuthUserInfoProfile(ui, profile) |
| 59 | + if ui.UID != "synthetic-openapi-id" || ui.OrgID != "synthetic-org" || ui.AvatarURL != "https://qoder.example/avatar" { |
| 60 | + t.Fatalf("applied profile = %+v", ui) |
| 61 | + } |
| 62 | + if ui.IsDataPolicyModifiable { |
| 63 | + t.Fatal("applied privacy-policy flag = true, want false") |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +func TestAuthUserInfoProfileKeepsLegacyShape(t *testing.T) { |
| 68 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 69 | + if r.URL.Path != "/api/v1/userinfo" { |
| 70 | + http.NotFound(w, r) |
| 71 | + return |
| 72 | + } |
| 73 | + w.Header().Set("Content-Type", "application/json") |
| 74 | + _, _ = w.Write([]byte(`{ |
| 75 | + "uid":"synthetic-legacy-uid", |
| 76 | + "name":"Legacy Name", |
| 77 | + "email":"legacy@example.invalid", |
| 78 | + "avatar_url":"https://qoder.example/legacy-avatar", |
| 79 | + "organization_id":"legacy-org", |
| 80 | + "organization_name":"Legacy Org", |
| 81 | + "organization_tags":["legacy-tag"], |
| 82 | + "data_policy_agreed":true, |
| 83 | + "is_data_policy_modifiable":true |
| 84 | + }`)) |
| 85 | + })) |
| 86 | + defer server.Close() |
| 87 | + |
| 88 | + manager := &authManager{ |
| 89 | + openapiBase: server.URL, |
| 90 | + httpc: server.Client(), |
| 91 | + logf: func(string, ...any) {}, |
| 92 | + } |
| 93 | + profile, err := manager.requestUserInfo(context.Background(), "synthetic-token") |
| 94 | + if err != nil { |
| 95 | + t.Fatalf("requestUserInfo() error = %v", err) |
| 96 | + } |
| 97 | + if profile.UID != "synthetic-legacy-uid" || profile.OrgID != "legacy-org" { |
| 98 | + t.Fatalf("legacy profile = %+v", profile) |
| 99 | + } |
| 100 | + if profile.DataPolicyAgreed == nil || !*profile.DataPolicyAgreed { |
| 101 | + t.Fatal("legacy data_policy_agreed was not decoded") |
| 102 | + } |
| 103 | + if profile.IsDataPolicyModifiable == nil || !*profile.IsDataPolicyModifiable { |
| 104 | + t.Fatal("legacy is_data_policy_modifiable was not decoded") |
| 105 | + } |
| 106 | + if len(profile.OrgTags) != 1 || profile.OrgTags[0] != "legacy-tag" { |
| 107 | + t.Fatalf("legacy organization tags = %v", profile.OrgTags) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +func TestAuthManagerPATLoginAcceptsCurrentGlobalUserInfoShape(t *testing.T) { |
| 112 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 113 | + w.Header().Set("Content-Type", "application/json") |
| 114 | + switch r.URL.Path { |
| 115 | + case "/api/v1/jobToken/exchange": |
| 116 | + _, _ = w.Write([]byte(`{"token":"synthetic-current-pat-token","refresh_token":"synthetic-current-pat-refresh","expires_in":7200}`)) |
| 117 | + case "/api/v1/userinfo": |
| 118 | + _, _ = w.Write([]byte(`{ |
| 119 | + "id":"synthetic-current-pat-user", |
| 120 | + "name":"Synthetic Current User", |
| 121 | + "username":"synthetic-current-login", |
| 122 | + "avatar":"https://qoder.example/current-avatar", |
| 123 | + "organization_id":"synthetic-current-org", |
| 124 | + "organization_name":"Synthetic Current Org", |
| 125 | + "is_privacy_policy_modifiable":false |
| 126 | + }`)) |
| 127 | + default: |
| 128 | + http.NotFound(w, r) |
| 129 | + } |
| 130 | + })) |
| 131 | + defer server.Close() |
| 132 | + |
| 133 | + generator := &fakeRuntimeFieldGenerator{output: runtimeFieldOutput{EncryptUserInfo: "synthetic-runtime-field", Key: "synthetic-runtime-key"}} |
| 134 | + factory := &fakeProtocolContextFactory{contexts: []protocolContext{&fakeProtocolContext{}}} |
| 135 | + codec := &fakeCredentialCodec{encrypted: "synthetic-encrypted-credential"} |
| 136 | + manager := syntheticAuthManager(t, &authManager{ |
| 137 | + protocol: &protocolServices{ |
| 138 | + credentials: codec, |
| 139 | + runtimeFields: generator, |
| 140 | + contextFactory: factory, |
| 141 | + }, |
| 142 | + authFile: filepath.Join(t.TempDir(), "user"), |
| 143 | + machineID: "0123456789abcdef-extra", |
| 144 | + openapiBase: server.URL, |
| 145 | + httpc: server.Client(), |
| 146 | + logf: func(string, ...any) {}, |
| 147 | + }) |
| 148 | + |
| 149 | + if err := patLogin(context.Background(), manager, "synthetic-pat"); err != nil { |
| 150 | + t.Fatalf("patLogin() error = %v", err) |
| 151 | + } |
| 152 | + if manager.ui == nil { |
| 153 | + t.Fatal("PAT login did not commit a user profile") |
| 154 | + } |
| 155 | + if manager.ui.UID != "synthetic-current-pat-user" { |
| 156 | + t.Fatalf("committed UID = %q, want current-shape id", manager.ui.UID) |
| 157 | + } |
| 158 | + if manager.ui.Name != "Synthetic Current User" || manager.ui.OrgID != "synthetic-current-org" { |
| 159 | + t.Fatalf("committed profile = %+v", manager.ui) |
| 160 | + } |
| 161 | + if generator.calls != 1 || len(factory.configs) != 1 { |
| 162 | + t.Fatalf("runtime field/context calls = %d/%d, want 1/1", generator.calls, len(factory.configs)) |
| 163 | + } |
| 164 | +} |
0 commit comments