summaryrefslogtreecommitdiff
path: root/tests/authorization/check_service_test.rs
blob: 73812fa1e7aacd585ddbef1b3db3c75b6453484d (plain)
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#[cfg(test)]
mod tests {
    use crate::support::factory_bot::*;
    use authzd::CheckService;
    use envoy_types::ext_authz::v3::pb::Authorization;
    use envoy_types::pb::envoy::service::auth::v3::attribute_context::HttpRequest;
    use std::collections::HashMap;
    use std::sync::Arc;

    fn subject() -> CheckService {
        CheckService::new(Arc::new(build_cedar_authorizer()))
    }

    #[tokio::test]
    async fn test_check_allows_valid_bearer_token() {
        let request = tonic::Request::new(build_request(|item: &mut HttpRequest| {
            item.path = String::from("/");
            item.headers = build_headers(vec![(
                "authorization".to_string(),
                format!("Bearer {}", String::from("valid-token")),
            )])
        }));

        let response = subject().check(request).await;
        assert!(response.is_ok());

        let check_response = response.unwrap().into_inner();
        assert!(check_response.status.is_some());

        let status = check_response.status.unwrap();
        assert_eq!(status.code, tonic::Code::Ok as i32);
    }

    #[tokio::test]
    async fn test_check_denies_invalid_bearer_token() {
        let request = tonic::Request::new(build_request(|item: &mut HttpRequest| {
            item.headers = HashMap::new();
        }));

        let response = subject().check(request).await;
        assert!(response.is_ok());

        let check_response = response.unwrap().into_inner();
        assert!(check_response.status.is_some());

        let status = check_response.status.unwrap();
        assert_eq!(status.code, tonic::Code::Unauthenticated as i32);
    }

    #[tokio::test]
    async fn test_static_assets() {
        let static_paths = vec![
            "app.js",
            "favicon.ico",
            "image.jpg",
            "index.html",
            "logo.png",
            "style.css",
        ];

        for path in static_paths {
            let request = tonic::Request::new(build_request(|item: &mut HttpRequest| {
                let method = String::from("GET");
                let host = String::from("sparkle.staging.runway.gitlab.net");

                item.method = method.clone();
                item.path = path.to_string();
                item.host = host.to_string();
                item.headers = build_headers(vec![
                    (String::from(":path"), path.to_string()),
                    (String::from(":method"), method),
                    (String::from(":authority"), host),
                ]);
            }));

            let response = subject().check(request).await;
            assert!(response.is_ok());

            let check_response = response.unwrap().into_inner();
            assert!(check_response.status.is_some());

            let status = check_response.status.unwrap();
            assert_eq!(status.code, tonic::Code::Ok as i32);
        }
    }

    #[tokio::test]
    async fn test_no_headers() {
        let request = tonic::Request::new(build_request(|_http| {}));

        let response = subject().check(request).await;
        assert!(response.is_ok());

        let check_response = response.unwrap().into_inner();
        assert!(check_response.status.is_some());

        let status = check_response.status.unwrap();
        assert_eq!(status.code, tonic::Code::Unauthenticated as i32);
    }

    #[tokio::test]
    async fn test_table() {
        let test_cases = vec![
            ("Bearer valid-token", tonic::Code::Ok),
            ("Bearer invalid-token", tonic::Code::Unauthenticated),
            ("Basic valid-token", tonic::Code::Unauthenticated),
            ("", tonic::Code::Unauthenticated),
        ];

        for (auth_value, expected_status_code) in test_cases {
            let request = tonic::Request::new(build_request(|item: &mut HttpRequest| {
                item.headers =
                    build_headers(vec![("authorization".to_string(), auth_value.to_string())]);
            }));

            let response = subject().check(request).await;
            assert!(response.is_ok());

            let check_response = response.unwrap().into_inner();
            let status = check_response.status.unwrap();

            assert_eq!(status.code, expected_status_code as i32);
        }
    }

    #[tokio::test]
    async fn test_public_sparkle_endpoints() {
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "GET", Path: "/application.js"}},
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "GET", Path: "/callback"}},
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "GET", Path: "/dashboard", Headers: loggedInHeaders}},
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "GET", Path: "/dashboard/nav"}},
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "GET", Path: "/favicon.ico"}},
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "GET", Path: "/favicon.png"}},
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "GET", Path: "/favicon.png"}},
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "GET", Path: "/health"}},
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "GET", Path: "/htmx.js"}},
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "GET", Path: "/index.html"}},
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "GET", Path: "/logo.png"}},
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "GET", Path: "/pico.min.css"}},
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "GET", Path: "/signout"}},
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "GET", Path: "/sparkles"}},
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "GET", Path: "/vue.global.js"}},
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "POST", Path: "/sparkles", Headers: loggedInHeaders}},
        // {status: tonic::Code::Ok, http: &HTTPRequest{Method: "POST", Path: "/sparkles/restore"}},
        // {status: tonic::Code::PermissionDenied, http: &HTTPRequest{Method: "GET", Path: "/dashboard"}},
        // {status: tonic::Code::PermissionDenied, http: &HTTPRequest{Method: "GET", Path: "/dashboard", Headers: invalidHeaders}},
        // {status: tonic::Code::PermissionDenied, http: &HTTPRequest{Method: "POST", Path: "/sparkles"}},

        let test_cases = vec![
            (
                "GET",
                "/",
                "sparkle.staging.runway.gitlab.net",
                tonic::Code::Ok,
            ),
            (
                "GET",
                "/application.js",
                "sparkle.staging.runway.gitlab.net",
                tonic::Code::Ok,
            ),
            (
                "GET",
                "/callback",
                "sparkle.staging.runway.gitlab.net",
                tonic::Code::Ok,
            ),
        ];

        for (method, path, host, expected_status_code) in test_cases {
            let request = tonic::Request::new(build_request(|item: &mut HttpRequest| {
                item.method = method.to_string();
                item.path = path.to_string();
                item.host = host.to_string();
                item.headers = build_headers(vec![
                    (String::from(":path"), path.to_string()),
                    (String::from(":method"), method.to_string()),
                    (String::from(":authority"), host.to_string()),
                ]);
            }));

            let response = subject().check(request).await;
            assert!(response.is_ok());

            let check_response = response.unwrap().into_inner();
            assert!(check_response.status.is_some());

            let status = check_response.status.unwrap();
            assert_eq!(status.code, expected_status_code as i32);
        }
    }
}