-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathper_session_auth.rs
More file actions
153 lines (137 loc) · 5.1 KB
/
Copy pathper_session_auth.rs
File metadata and controls
153 lines (137 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
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
use std::sync::Arc;
use github_copilot_sdk::SessionConfig;
use github_copilot_sdk::handler::ApproveAllHandler;
use super::support::with_e2e_context;
#[tokio::test]
async fn session_uses_client_token_when_no_session_token_is_supplied() {
with_e2e_context(
"per-session-auth",
"session_uses_client_token_when_no_session_token_is_supplied",
|ctx| {
Box::pin(async move {
let token = "alice-token";
ctx.set_copilot_user_by_token_with_login(token, "alice");
let client = github_copilot_sdk::Client::start(
ctx.client_options().with_github_token(token),
)
.await
.expect("start client");
let session = client
.create_session(
SessionConfig::default()
.with_permission_handler(Arc::new(ApproveAllHandler)),
)
.await
.expect("create session");
let status = session
.rpc()
.git_hub_auth()
.get_status()
.await
.expect("auth status");
assert!(status.is_authenticated);
assert_eq!(status.login.as_deref(), Some("alice"));
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn session_token_overrides_client_token() {
with_e2e_context(
"per-session-auth",
"session_token_overrides_client_token",
|ctx| {
Box::pin(async move {
ctx.set_copilot_user_by_token_with_login("alice-token", "alice");
ctx.set_copilot_user_by_token_with_login("bob-token", "bob");
let client = github_copilot_sdk::Client::start(
ctx.client_options().with_github_token("alice-token"),
)
.await
.expect("start client");
let session = client
.create_session(
SessionConfig::default()
.with_permission_handler(Arc::new(ApproveAllHandler))
.with_github_token("bob-token"),
)
.await
.expect("create session");
let status = session
.rpc()
.git_hub_auth()
.get_status()
.await
.expect("auth status");
assert!(status.is_authenticated);
assert_eq!(status.login.as_deref(), Some("bob"));
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn session_auth_status_is_unauthenticated_without_token() {
with_e2e_context(
"per-session-auth",
"session_auth_status_is_unauthenticated_without_token",
|ctx| {
Box::pin(async move {
let client = ctx.start_client().await;
let session = client
.create_session(
SessionConfig::default()
.with_permission_handler(Arc::new(ApproveAllHandler)),
)
.await
.expect("create session");
let status = session
.rpc()
.git_hub_auth()
.get_status()
.await
.expect("auth status");
assert!(!status.is_authenticated);
assert!(status.login.is_none());
session.disconnect().await.expect("disconnect session");
client.stop().await.expect("stop client");
})
},
)
.await;
}
#[tokio::test]
async fn session_fails_with_invalid_token() {
with_e2e_context(
"per-session-auth",
"session_fails_with_invalid_token",
|ctx| {
Box::pin(async move {
ctx.set_copilot_user_by_token_with_login("valid-token", "valid-user");
let client = ctx.start_client().await;
let err = match client
.create_session(
SessionConfig::default()
.with_permission_handler(Arc::new(ApproveAllHandler))
.with_github_token("invalid-token"),
)
.await
{
Ok(_) => panic!("invalid token should fail session create"),
Err(err) => err,
};
assert!(
err.to_string().contains("401") || err.to_string().contains("Unauthorized"),
"expected unauthorized error, got {err}"
);
client.stop().await.expect("stop client");
})
},
)
.await;
}