-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create DB table to store SMTP creds, and GraphQL API endpoints …
…to get and set them (#1176) * feat: Create DB table to store SMTP creds, and GraphQL API endpoints to get and set them * Apply suggested changes * [autofix.ci] apply automated fixes * [autofix.ci] apply automated fixes (attempt 2/3) * Add test, change field and struct names * Fix table name in update query * Fix method name * Forgot to unwrap Option * Remove trailing comma in SQL query * Update row indices to be zero-indexed * Apply suggested changes * Code cleanup * Use insert-or-update instead of delete and insert * Fix where clause * Update ee/tabby-db/migrations/07-email-service-credentials/down.sql --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> Co-authored-by: Meng Zhang <[email protected]>
- Loading branch information
1 parent
ef7674c
commit 976fe70
Showing
4 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE email_service_credential; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
CREATE TABLE email_service_credential( | ||
id INTEGER PRIMARY KEY AUTOINCREMENT, | ||
smtp_username VARCHAR(255), | ||
smtp_password VARCHAR(255), | ||
smtp_server VARCHAR(255) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
use anyhow::Result; | ||
use rusqlite::{named_params, OptionalExtension}; | ||
|
||
use crate::DbConn; | ||
|
||
const EMAIL_CREDENTIAL_ROW_ID: i32 = 1; | ||
|
||
#[derive(Debug, PartialEq)] | ||
pub struct EmailServiceCredentialDAO { | ||
pub smtp_username: String, | ||
pub smtp_password: String, | ||
pub smtp_server: String, | ||
} | ||
|
||
impl EmailServiceCredentialDAO { | ||
fn new(smtp_username: String, smtp_password: String, smtp_server: String) -> Self { | ||
Self { | ||
smtp_username, | ||
smtp_password, | ||
smtp_server, | ||
} | ||
} | ||
} | ||
|
||
impl DbConn { | ||
pub async fn read_email_service_credential(&self) -> Result<Option<EmailServiceCredentialDAO>> { | ||
let res = self | ||
.conn | ||
.call(|c| { | ||
Ok(c.query_row( | ||
"SELECT smtp_username, smtp_password, smtp_server FROM email_service_credential WHERE id=?", | ||
[EMAIL_CREDENTIAL_ROW_ID], | ||
|row| Ok(EmailServiceCredentialDAO::new(row.get(0)?, row.get(1)?, row.get(2)?)), | ||
) | ||
.optional()) | ||
}) | ||
.await?; | ||
Ok(res?) | ||
} | ||
|
||
pub async fn update_email_service_credential( | ||
&self, | ||
creds: EmailServiceCredentialDAO, | ||
) -> Result<()> { | ||
Ok(self | ||
.conn | ||
.call(move |c| { | ||
c.execute("INSERT INTO email_service_credential VALUES (:id, :user, :pass, :server) | ||
ON CONFLICT(id) DO UPDATE SET smtp_username = :user, smtp_password = :pass, smtp_server = :server | ||
WHERE id = :id", | ||
named_params! { | ||
":id": EMAIL_CREDENTIAL_ROW_ID, | ||
":user": creds.smtp_username, | ||
":pass": creds.smtp_password, | ||
":server": creds.smtp_server, | ||
} | ||
)?; | ||
Ok(()) | ||
}) | ||
.await?) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[tokio::test] | ||
async fn test_smtp_info() { | ||
let conn = DbConn::new_in_memory().await.unwrap(); | ||
|
||
// Test no credentials prior to insertion | ||
assert_eq!(conn.read_email_service_credential().await.unwrap(), None); | ||
|
||
// Test insertion | ||
conn.update_email_service_credential(EmailServiceCredentialDAO::new( | ||
"user".into(), | ||
"pass".into(), | ||
"server".into(), | ||
)) | ||
.await | ||
.unwrap(); | ||
|
||
let creds = conn.read_email_service_credential().await.unwrap().unwrap(); | ||
assert_eq!(creds.smtp_username, "user"); | ||
assert_eq!(creds.smtp_password, "pass"); | ||
assert_eq!(creds.smtp_server, "server"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters