|
1 | | -use cipherstash_client::ZeroKMSConfig; |
2 | 1 | use cipherstash_dynamodb::{ |
3 | | - encrypted_table::Dynamo, Decryptable, Encryptable, EncryptedTable, Identifiable, Searchable, |
| 2 | + Decryptable, Encryptable, Identifiable, Searchable, |
4 | 3 | }; |
5 | | -use miette::IntoDiagnostic; |
| 4 | +use common::{check_eq, check_err, check_none, fail_not_found, secondary_dataset_id, with_encrypted_table}; |
6 | 5 | use uuid::Uuid; |
7 | 6 | mod common; |
8 | 7 |
|
@@ -196,86 +195,50 @@ fn build_test_record(email: &str, name: &str) -> Crazy { |
196 | 195 | } |
197 | 196 | } |
198 | 197 |
|
199 | | -async fn init_table() -> EncryptedTable<Dynamo> { |
200 | | - let config = aws_config::from_env() |
201 | | - .endpoint_url("http://localhost:8000") |
202 | | - .load() |
203 | | - .await; |
204 | | - |
205 | | - let client = aws_sdk_dynamodb::Client::new(&config); |
206 | | - |
207 | | - let table_name = "crazy-record"; |
208 | | - |
209 | | - common::create_table(&client, table_name).await; |
210 | | - |
211 | | - EncryptedTable::init(client, table_name) |
212 | | - .await |
213 | | - .expect("Failed to init table") |
214 | | -} |
215 | | - |
216 | 198 | #[tokio::test] |
217 | 199 | async fn test_round_trip() -> Result<(), Box<dyn std::error::Error>> { |
218 | | - let table = init_table().await; |
219 | | - let record = build_test_record("[email protected]", "Dan"); |
220 | | - table.put(record.clone()).await.into_diagnostic()?; |
221 | | - |
222 | | - let s: Crazy = table |
223 | | - .get(("[email protected]", "Dan")) |
224 | | - .await |
225 | | - .into_diagnostic()? |
226 | | - .unwrap(); |
227 | | - |
228 | | - assert_eq!(s, record); |
229 | | - |
230 | | - Ok(()) |
| 200 | + with_encrypted_table("round-trip", |table| async move { |
| 201 | + let record = build_test_record("[email protected]", "Dan"); |
| 202 | + table.put(record.clone()).await?; |
| 203 | + |
| 204 | + let s: Crazy = table |
| 205 | + .get(("[email protected]", "Dan")) |
| 206 | + .await? |
| 207 | + .ok_or(fail_not_found())?; |
| 208 | + |
| 209 | + check_eq(s, record) |
| 210 | + }) |
| 211 | + .await |
231 | 212 | } |
232 | 213 |
|
233 | 214 | #[tokio::test] |
234 | 215 | async fn test_invalid_dataset() -> Result<(), Box<dyn std::error::Error>> { |
235 | | - let table = init_table().await; |
236 | | - let record = build_test_record("[email protected]", "Dan"); |
| 216 | + with_encrypted_table("round-trip", |table| async move { |
| 217 | + let record = build_test_record("[email protected]", "Dan"); |
237 | 218 |
|
238 | | - // A random UUID doesn't exist |
239 | | - assert_err!(table.put_via(record.clone(), Uuid::new_v4()).await); |
240 | | - |
241 | | - Ok(()) |
| 219 | + // A random UUID doesn't exist |
| 220 | + check_err(table.put_via(record.clone(), Uuid::new_v4()).await) |
| 221 | + }) |
| 222 | + .await |
242 | 223 | } |
243 | 224 |
|
244 | 225 | #[tokio::test] |
245 | | -async fn test_invalid_specific_dataset() -> miette::Result<()> { |
246 | | - // TODO: Load client ID from env |
247 | | - let client_id = Uuid::parse_str("b91e5b26-f21f-4694-8bce-c61c10e42301").into_diagnostic()?; |
248 | | - let client = ZeroKMSConfig::builder() |
249 | | - .with_env() |
250 | | - .build() |
251 | | - .into_diagnostic()? |
252 | | - .create_client(); |
253 | | - |
254 | | - let dataset = client |
255 | | - .create_dataset("test-dataset", "Test dataset") |
256 | | - .await |
257 | | - .into_diagnostic()?; |
258 | | - |
259 | | - // Grant ourselves access to the dataset |
260 | | - client |
261 | | - .grant_dataset(client_id, dataset.id) |
262 | | - .await |
263 | | - .into_diagnostic()?; |
264 | | - |
265 | | - let table = init_table().await; |
266 | | - let record = build_test_record("[email protected]", "Person"); |
267 | | - |
268 | | - table.put_via(record.clone(), dataset.id).await?; |
269 | | - |
270 | | - let s: Crazy = table |
271 | | - .get_via(("[email protected]", "Person"), dataset .id) |
272 | | - .await? |
273 | | - .unwrap(); |
274 | | - |
275 | | - assert_eq!(s, record); |
276 | | - |
277 | | - // Test that we can't get the record via the default dataset |
278 | | - assert_none!(table .get ::< Crazy> (("[email protected]", "Person")).await? ); |
279 | | - |
280 | | - Ok(()) |
| 226 | +async fn test_invalid_specific_dataset() -> Result<(), Box<dyn std::error::Error>> { |
| 227 | + with_encrypted_table("round-trip", |table| async move { |
| 228 | + let record = build_test_record("[email protected]", "Person"); |
| 229 | + table |
| 230 | + .put_via(record.clone(), secondary_dataset_id()) |
| 231 | + .await?; |
| 232 | + |
| 233 | + let s: Crazy = table |
| 234 | + .get_via(("[email protected]", "Person"), secondary_dataset_id()) |
| 235 | + .await? |
| 236 | + .ok_or(fail_not_found())?; |
| 237 | + |
| 238 | + check_eq(s, record)?; |
| 239 | + |
| 240 | + // Test that we can't get the record via the default dataset |
| 241 | + check_none(table .get::<Crazy>(("[email protected]", "Person")).await? ) |
| 242 | + }) |
| 243 | + .await |
281 | 244 | } |
0 commit comments