Skip to content

Commit 1379821

Browse files
Hocurilink2xt
andauthored
refactor: Move logins into SQL table (#6724)
Move all `configured_*` parameters into a new SQL table `transports`. All `configured_*` parameters are deprecated; the only exception is `configured_addr`, which is used to store the address of the primary transport. Currently, there can only ever be one primary transport (i.e. the `transports` table only ever has one row); this PR is not supposed to change DC's behavior in any meaningful way. This is a preparation for mt. --------- Co-authored-by: l <link2xt@testrun.org>
1 parent 1722cb8 commit 1379821

File tree

10 files changed

+344
-125
lines changed

10 files changed

+344
-125
lines changed

deltachat-rpc-client/tests/test_something.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -713,12 +713,11 @@ def test_get_http_response(acfactory):
713713

714714
def test_configured_imap_certificate_checks(acfactory):
715715
alice = acfactory.new_configured_account()
716-
configured_certificate_checks = alice.get_config("configured_imap_certificate_checks")
717716

718717
# Certificate checks should be configured (not None)
719-
assert configured_certificate_checks
718+
assert "cert_automatic" in alice.get_info().used_account_settings
720719

721-
# 0 is the value old Delta Chat core versions used
720+
# "cert_old_automatic" is the value old Delta Chat core versions used
722721
# to mean user entered "imap_certificate_checks=0" (Automatic)
723722
# and configuration failed to use strict TLS checks
724723
# so it switched strict TLS checks off.
@@ -729,7 +728,7 @@ def test_configured_imap_certificate_checks(acfactory):
729728
#
730729
# Core 1.142.4, 1.142.5 and 1.142.6 saved this value due to bug.
731730
# This test is a regression test to prevent this happening again.
732-
assert configured_certificate_checks != "0"
731+
assert "cert_old_automatic" not in alice.get_info().used_account_settings
733732

734733

735734
def test_no_old_msg_is_fresh(acfactory):

python/src/deltachat/testplugin.py

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -482,12 +482,8 @@ def get_pseudo_configured_account(self, passphrase: Optional[str] = None) -> Acc
482482
addr = f"{acname}@offline.org"
483483
ac.update_config(
484484
{
485-
"addr": addr,
486-
"displayname": acname,
487-
"mail_pw": "123",
488485
"configured_addr": addr,
489-
"configured_mail_pw": "123",
490-
"configured": "1",
486+
"displayname": acname,
491487
},
492488
)
493489
self._preconfigure_key(ac)

python/tests/test_1_online.py

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1502,15 +1502,6 @@ def test_connectivity(acfactory, lp):
15021502
assert len(msgs) == 2
15031503
assert msgs[1].text == "Hi 2"
15041504

1505-
lp.sec("Test that the connectivity is NOT_CONNECTED if the password is wrong")
1506-
1507-
ac1.set_config("configured_mail_pw", "abc")
1508-
ac1.stop_io()
1509-
ac1._evtracker.wait_for_connectivity(dc.const.DC_CONNECTIVITY_NOT_CONNECTED)
1510-
ac1.start_io()
1511-
ac1._evtracker.wait_for_connectivity(dc.const.DC_CONNECTIVITY_CONNECTING)
1512-
ac1._evtracker.wait_for_connectivity(dc.const.DC_CONNECTIVITY_NOT_CONNECTED)
1513-
15141505

15151506
def test_fetch_deleted_msg(acfactory, lp):
15161507
"""This is a regression test: Messages with \\Deleted flag were downloaded again and again,

src/config.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::env;
44
use std::path::Path;
55
use std::str::FromStr;
66

7-
use anyhow::{ensure, Context as _, Result};
7+
use anyhow::{bail, ensure, Context as _, Result};
88
use base64::Engine as _;
99
use deltachat_contact_tools::{addr_cmp, sanitize_single_line};
1010
use serde::{Deserialize, Serialize};
@@ -13,10 +13,12 @@ use strum_macros::{AsRefStr, Display, EnumIter, EnumString};
1313
use tokio::fs;
1414

1515
use crate::blob::BlobObject;
16+
use crate::configure::EnteredLoginParam;
1617
use crate::constants;
1718
use crate::context::Context;
1819
use crate::events::EventType;
1920
use crate::log::LogExt;
21+
use crate::login_param::ConfiguredLoginParam;
2022
use crate::mimefactory::RECOMMENDED_FILE_SIZE;
2123
use crate::provider::{get_provider_by_id, Provider};
2224
use crate::sync::{self, Sync::*, SyncData};
@@ -525,21 +527,22 @@ impl Context {
525527
// Default values
526528
let val = match key {
527529
Config::BccSelf => match Box::pin(self.is_chatmail()).await? {
528-
false => Some("1"),
529-
true => Some("0"),
530+
false => Some("1".to_string()),
531+
true => Some("0".to_string()),
530532
},
531-
Config::ConfiguredInboxFolder => Some("INBOX"),
533+
Config::ConfiguredInboxFolder => Some("INBOX".to_string()),
532534
Config::DeleteServerAfter => {
533535
match !Box::pin(self.get_config_bool(Config::BccSelf)).await?
534536
&& Box::pin(self.is_chatmail()).await?
535537
{
536-
true => Some("1"),
537-
false => Some("0"),
538+
true => Some("1".to_string()),
539+
false => Some("0".to_string()),
538540
}
539541
}
540-
_ => key.get_str("default"),
542+
Config::Addr => self.get_config_opt(Config::ConfiguredAddr).await?,
543+
_ => key.get_str("default").map(|s| s.to_string()),
541544
};
542-
Ok(val.map(|s| s.to_string()))
545+
Ok(val)
543546
}
544547

545548
/// Returns Some(T) if a value for the given key is set and was successfully parsed.
@@ -805,6 +808,19 @@ impl Context {
805808
.set_raw_config(constants::DC_FOLDERS_CONFIGURED_KEY, None)
806809
.await?;
807810
}
811+
Config::ConfiguredAddr => {
812+
if self.is_configured().await? {
813+
bail!("Cannot change ConfiguredAddr");
814+
}
815+
if let Some(addr) = value {
816+
info!(self, "Creating a pseudo configured account which will not be able to send or receive messages. Only meant for tests!");
817+
ConfiguredLoginParam::from_json(&format!(
818+
r#"{{"addr":"{addr}","imap":[],"imap_user":"","imap_password":"","smtp":[],"smtp_user":"","smtp_password":"","certificate_checks":"Automatic","oauth2":false}}"#
819+
))?
820+
.save_to_transports_table(self, &EnteredLoginParam::default())
821+
.await?;
822+
}
823+
}
808824
_ => {
809825
self.sql.set_raw_config(key.as_ref(), value).await?;
810826
}
@@ -891,6 +907,7 @@ impl Context {
891907
/// primary address (if exists) as a secondary address.
892908
///
893909
/// This should only be used by test code and during configure.
910+
#[cfg(test)] // AEAP is disabled, but there are still tests for it
894911
pub(crate) async fn set_primary_self_addr(&self, primary_new: &str) -> Result<()> {
895912
self.quota.write().await.take();
896913

@@ -904,7 +921,8 @@ impl Context {
904921
)
905922
.await?;
906923

907-
self.set_config_internal(Config::ConfiguredAddr, Some(primary_new))
924+
self.sql
925+
.set_raw_config(Config::ConfiguredAddr.as_ref(), Some(primary_new))
908926
.await?;
909927
self.emit_event(EventType::ConnectivityChanged);
910928
Ok(())

src/configure.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::login_param::{
3535
};
3636
use crate::message::Message;
3737
use crate::oauth2::get_oauth2_addr;
38-
use crate::provider::{Protocol, Socket, UsernamePattern};
38+
use crate::provider::{Protocol, Provider, Socket, UsernamePattern};
3939
use crate::qr::set_account_from_qr;
4040
use crate::smtp::Smtp;
4141
use crate::sync::Sync::*;
@@ -63,7 +63,7 @@ macro_rules! progress {
6363
impl Context {
6464
/// Checks if the context is already configured.
6565
pub async fn is_configured(&self) -> Result<bool> {
66-
self.sql.get_raw_config_bool("configured").await
66+
self.sql.exists("SELECT COUNT(*) FROM transports", ()).await
6767
}
6868

6969
/// Configures this account with the currently provided parameters.
@@ -181,9 +181,21 @@ impl Context {
181181
/// Use [Self::add_transport()] to add or change a transport
182182
/// and [Self::delete_transport()] to delete a transport.
183183
pub async fn list_transports(&self) -> Result<Vec<EnteredLoginParam>> {
184-
let param = EnteredLoginParam::load(self).await?;
184+
let transports = self
185+
.sql
186+
.query_map(
187+
"SELECT entered_param FROM transports",
188+
(),
189+
|row| row.get::<_, String>(0),
190+
|rows| {
191+
rows.flatten()
192+
.map(|s| Ok(serde_json::from_str(&s)?))
193+
.collect::<Result<Vec<EnteredLoginParam>>>()
194+
},
195+
)
196+
.await?;
185197

186-
Ok(vec![param])
198+
Ok(transports)
187199
}
188200

189201
/// Removes the transport with the specified email address
@@ -197,20 +209,20 @@ impl Context {
197209
info!(self, "Configure ...");
198210

199211
let old_addr = self.get_config(Config::ConfiguredAddr).await?;
200-
let configured_param = configure(self, param).await?;
212+
let provider = configure(self, param).await?;
201213
self.set_config_internal(Config::NotifyAboutWrongPw, Some("1"))
202214
.await?;
203-
on_configure_completed(self, configured_param, old_addr).await?;
215+
on_configure_completed(self, provider, old_addr).await?;
204216
Ok(())
205217
}
206218
}
207219

208220
async fn on_configure_completed(
209221
context: &Context,
210-
param: ConfiguredLoginParam,
222+
provider: Option<&'static Provider>,
211223
old_addr: Option<String>,
212224
) -> Result<()> {
213-
if let Some(provider) = param.provider {
225+
if let Some(provider) = provider {
214226
if let Some(config_defaults) = provider.config_defaults {
215227
for def in config_defaults {
216228
if !context.config_exists(def.key).await? {
@@ -446,7 +458,7 @@ async fn get_configured_param(
446458
Ok(configured_login_param)
447459
}
448460

449-
async fn configure(ctx: &Context, param: &EnteredLoginParam) -> Result<ConfiguredLoginParam> {
461+
async fn configure(ctx: &Context, param: &EnteredLoginParam) -> Result<Option<&'static Provider>> {
450462
progress!(ctx, 1);
451463

452464
let ctx2 = ctx.clone();
@@ -556,7 +568,11 @@ async fn configure(ctx: &Context, param: &EnteredLoginParam) -> Result<Configure
556568
}
557569
}
558570

559-
configured_param.save_as_configured_params(ctx).await?;
571+
let provider = configured_param.provider;
572+
configured_param
573+
.save_to_transports_table(ctx, param)
574+
.await?;
575+
560576
ctx.set_config_internal(Config::ConfiguredTimestamp, Some(&time().to_string()))
561577
.await?;
562578

@@ -572,7 +588,7 @@ async fn configure(ctx: &Context, param: &EnteredLoginParam) -> Result<Configure
572588
ctx.sql.set_raw_config_bool("configured", true).await?;
573589
ctx.emit_event(EventType::AccountsItemChanged);
574590

575-
Ok(configured_param)
591+
Ok(provider)
576592
}
577593

578594
/// Retrieve available autoconfigurations.

0 commit comments

Comments
 (0)