| 1 |
/* This Source Code Form is subject to the terms of the Mozilla Public |
| 2 |
* License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 |
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 4 |
|
| 5 |
use std::collections::HashMap; |
| 6 |
|
| 7 |
use remote_settings::Attachment; |
| 8 |
use serde_json::json; |
| 9 |
use serde_json::Value as JsonValue; |
| 10 |
|
| 11 |
use crate::{ |
| 12 |
error::Error, |
| 13 |
rs::{Client, Collection, Record, SuggestRecordId, SuggestRecordType}, |
| 14 |
testing::JsonExt, |
| 15 |
Result, |
| 16 |
}; |
| 17 |
|
| 18 |
/// Mock remote settings client |
| 19 |
/// |
| 20 |
/// MockRemoteSettingsClient uses the builder pattern for its API: most methods input `self` and |
| 21 |
/// return a modified version of it. |
| 22 |
pub struct MockRemoteSettingsClient { |
| 23 |
pub records: Vec<Record>, |
| 24 |
pub attachments: HashMap<String, Vec<u8>>, |
| 25 |
pub last_modified_timestamp: u64, |
| 26 |
} |
| 27 |
|
| 28 |
impl Default for MockRemoteSettingsClient { |
| 29 |
fn default() -> Self { |
| 30 |
Self { |
| 31 |
records: Vec::new(), |
| 32 |
attachments: HashMap::new(), |
| 33 |
last_modified_timestamp: 100, |
| 34 |
} |
| 35 |
} |
| 36 |
} |
| 37 |
|
| 38 |
impl MockRemoteSettingsClient { |
| 39 |
// Consuming Builder API, this is best for constructing the initial client |
| 40 |
pub fn with_record(mut self, record: MockRecord) -> Self { |
| 41 |
self.add_record(record); |
| 42 |
self |
| 43 |
} |
| 44 |
|
| 45 |
// Non-Consuming Builder API, this is best for updating an existing client |
| 46 |
|
| 47 |
/// Add a record to the mock data |
| 48 |
pub fn add_record(&mut self, mock_record: MockRecord) -> &mut Self { |
| 49 |
self.insert_attachment(&mock_record); |
| 50 |
self.records.push(self.record_from_mock(mock_record)); |
| 51 |
self |
| 52 |
} |
| 53 |
|
| 54 |
// Update API, these use the &mut builder pattern, since they're used with already built |
| 55 |
// clients |
| 56 |
|
| 57 |
/// Update a record, storing a new payload and bumping the modified time |
| 58 |
pub fn update_record(&mut self, mock_record: MockRecord) -> &mut Self { |
| 59 |
let index = self |
| 60 |
.records |
| 61 |
.iter() |
| 62 |
.position(|r| mock_record.matches_record(r)) |
| 63 |
.unwrap_or_else(|| panic!("update_record: {} not found", mock_record.qualified_id())); |
| 64 |
|
| 65 |
self.insert_attachment(&mock_record); |
| 66 |
|
| 67 |
let mut record = self.record_from_mock(mock_record); |
| 68 |
record.last_modified += 1; |
| 69 |
self.records.splice(index..=index, std::iter::once(record)); |
| 70 |
|
| 71 |
self |
| 72 |
} |
| 73 |
|
| 74 |
/// Delete a record and its attachment |
| 75 |
pub fn delete_record(&mut self, mock_record: MockRecord) -> &mut Self { |
| 76 |
let index = self |
| 77 |
.records |
| 78 |
.iter() |
| 79 |
.position(|r| mock_record.matches_record(r)) |
| 80 |
.unwrap_or_else(|| panic!("delete_record: {} not found", mock_record.qualified_id())); |
| 81 |
self.records.remove(index); |
| 82 |
self.attachments.remove(&mock_record.qualified_id()); |
| 83 |
self |
| 84 |
} |
| 85 |
|
| 86 |
pub fn insert_attachment(&mut self, mock_record: &MockRecord) { |
| 87 |
if let Some(bytes) = mock_record.attachment.as_ref().map(|a| match a { |
| 88 |
MockAttachment::Json(items) => serde_json::to_vec(&items).unwrap_or_else(|_| { |
| 89 |
panic!( |
| 90 |
"error serializing attachment data: {}", |
| 91 |
mock_record.qualified_id() |
| 92 |
) |
| 93 |
}), |
| 94 |
MockAttachment::Icon(icon) => icon.data.as_bytes().to_vec(), |
| 95 |
}) { |
| 96 |
self.attachments.insert(mock_record.qualified_id(), bytes); |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
fn record_from_mock(&self, mock_record: MockRecord) -> Record { |
| 101 |
let mut record: Record = mock_record.into(); |
| 102 |
record.last_modified = self.last_modified_timestamp; |
| 103 |
record |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
impl Client for MockRemoteSettingsClient { |
| 108 |
fn get_records(&self, collection: Collection) -> Result<Vec<Record>> { |
| 109 |
Ok(self |
| 110 |
.records |
| 111 |
.iter() |
| 112 |
.filter(|r| collection == r.collection) |
| 113 |
.cloned() |
| 114 |
.collect()) |
| 115 |
} |
| 116 |
|
| 117 |
fn download_attachment(&self, record: &Record) -> Result<Vec<u8>> { |
| 118 |
match &record.attachment { |
| 119 |
None => Err(Error::MissingAttachment(record.id.to_string())), |
| 120 |
Some(a) => Ok(self |
| 121 |
.attachments |
| 122 |
.get(&a.location) |
| 123 |
.expect("Attachment not in hash map") |
| 124 |
.clone()), |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
pub struct MockRecord { |
| 130 |
pub collection: Collection, |
| 131 |
pub record_type: SuggestRecordType, |
| 132 |
pub id: String, |
| 133 |
pub inline_data: Option<JsonValue>, |
| 134 |
pub attachment: Option<MockAttachment>, |
| 135 |
} |
| 136 |
|
| 137 |
impl MockRecord { |
| 138 |
pub fn qualified_id(&self) -> String { |
| 139 |
format!("{}:{}", self.collection.name(), self.id) |
| 140 |
} |
| 141 |
|
| 142 |
fn matches_record(&self, record: &Record) -> bool { |
| 143 |
self.collection == record.collection && self.id.as_str() == record.id.as_str() |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
impl From<MockRecord> for Record { |
| 148 |
fn from(mock_record: MockRecord) -> Self { |
| 149 |
let attachment = mock_record.attachment.as_ref().map(|a| match a { |
| 150 |
MockAttachment::Json(_) => Attachment { |
| 151 |
filename: mock_record.id.to_string(), |
| 152 |
location: mock_record.qualified_id(), |
| 153 |
mimetype: "application/json".into(), |
| 154 |
hash: "".into(), |
| 155 |
size: 0, |
| 156 |
}, |
| 157 |
MockAttachment::Icon(icon) => Attachment { |
| 158 |
filename: mock_record.id.to_string(), |
| 159 |
location: mock_record.qualified_id(), |
| 160 |
mimetype: icon.mimetype.to_string(), |
| 161 |
hash: "".into(), |
| 162 |
size: 0, |
| 163 |
}, |
| 164 |
}); |
| 165 |
|
| 166 |
Self { |
| 167 |
id: SuggestRecordId::new(mock_record.id), |
| 168 |
collection: mock_record.collection, |
| 169 |
last_modified: 0, |
| 170 |
payload: serde_json::from_value( |
| 171 |
json!({ |
| 172 |
"type": mock_record.record_type.as_str(), |
| 173 |
}) |
| 174 |
.merge(mock_record.inline_data.unwrap_or(json!({}))), |
| 175 |
) |
| 176 |
.unwrap(), |
| 177 |
attachment, |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
|
| 182 |
pub enum MockAttachment { |
| 183 |
Json(JsonValue), |
| 184 |
Icon(MockIcon), |
| 185 |
} |
| 186 |
|
| 187 |
pub struct MockIcon { |
| 188 |
pub id: &'static str, |
| 189 |
pub data: &'static str, |
| 190 |
pub mimetype: &'static str, |
| 191 |
} |