Skip to content

Commit

Permalink
feat: Create DB table to store SMTP creds, and GraphQL API endpoints …
Browse files Browse the repository at this point in the history
…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
3 people authored Jan 9, 2024
1 parent ef7674c commit 976fe70
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE email_service_credential;
6 changes: 6 additions & 0 deletions ee/tabby-db/migrations/07-email-service-credentials/up.sql
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)
);
89 changes: 89 additions & 0 deletions ee/tabby-db/src/email_service_credential.rs
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");
}
}
2 changes: 2 additions & 0 deletions ee/tabby-db/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
pub use email_service_credential::EmailServiceCredentialDAO;
pub use github_oauth_credential::GithubOAuthCredentialDAO;
pub use invitations::InvitationDAO;
pub use job_runs::JobRunDAO;
pub use users::UserDAO;

mod email_service_credential;
mod github_oauth_credential;
mod invitations;
mod job_runs;
Expand Down

0 comments on commit 976fe70

Please sign in to comment.