Skip to content
Snippets Groups Projects
Commit 2be20182 authored by Bahtiar Gadimov's avatar Bahtiar Gadimov
Browse files

Add EncryptionState

parent ee79762e
No related branches found
No related tags found
No related merge requests found
# -*- coding: utf-8 -*-
#
# Copyright 2015 Bahtiar `kalkin-` Gadimov <bahtiar@gadimov.de>
# Copyright 2015 Daniel Gultsch <daniel@cgultsch.de>
#
# This file is part of Gajim-OMEMO plugin.
#
# The Gajim-OMEMO plugin is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# Gajim-OMEMO is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along with
# the Gajim-OMEMO plugin. If not, see <http://www.gnu.org/licenses/>.
#
class EncryptionState():
""" Used to store if OMEMO is enabled or not between gajim restarts """
def __init__(self, dbConn):
"""
:type dbConn: Connection
"""
self.dbConn = dbConn
dbConn.execute(""" CREATE TABLE IF NOT EXISTS encryption_state (
jid TEXT PRIMARY KEY,
encryption INTEGER,
timestamp NUMBER DEFAULT CURRENT_TIMESTAMP
) WITHOUT ROWID;
""")
def activate(self, jid):
q = """INSERT OR REPLACE INTO encryption_state (jid, encryption)
VALUES (?, 1) """
c = self.dbConn.cursor()
c.execute(q, (jid,))
self.dbConn.commit()
def deactivate(self, jid):
q = """INSERT OR REPLACE INTO encryption_state (jid, encryption)
VALUES (?, 0)"""
c = self.dbConn.cursor()
c.execute(q, (jid,))
self.dbConn.commit()
def is_active(self, jid):
q = 'SELECT encryption FROM encryption_state where jid = ?;'
c = self.dbConn.cursor()
c.execute(q, (jid,))
result = c.fetchone()
if result is None:
return False
return result[0] == 1
import unittest
from store.encryption import EncryptionState
import os
import sqlite3
class TestEncryptionStateStore(unittest.TestCase):
def setUp(self):
self.conn = sqlite3.connect('test-db', check_same_thread=False)
self.e = EncryptionState(self.conn)
def test_create(self):
self.assertEquals(self.e.is_active('romeo@example.com'), False)
def test_enable_encryption(self):
self.e.activate('romeo@example.com')
self.assertEquals(self.e.is_active('romeo@example.com'), True)
def test_disable_encryption(self):
self.e.activate('romeo@example.com')
self.assertEquals(self.e.is_active('romeo@example.com'), True)
self.e.deactivate('romeo@example.com')
self.assertEquals(self.e.is_active('romeo@example.com'), False)
def tearDown(self):
os.remove('test-db')
if __name__ == '__main__':
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment