From 42037502ab7c28f8b768f07f992f2b34da7df8f9 Mon Sep 17 00:00:00 2001
From: navjot <navjot.g@cisinlabs.com>
Date: Tue, 8 Sep 2020 15:25:26 +0530
Subject: [PATCH] fixed Own address should not save in address book issue

- removed redundant code

- written test case for address book own address saving

- fixed CQ issues

- added helper_addressbook module

- Fixed CQ issue of src.helper_addressbook module

- fixed travis-ci checks failing issue
---
 src/bitmessageqt/__init__.py  | 13 ++++++++-----
 src/helper_addressbook.py     | 11 +++++++++++
 src/tests/test_addressbook.py | 20 ++++++++++++++++++++
 3 files changed, 39 insertions(+), 5 deletions(-)
 create mode 100644 src/helper_addressbook.py
 create mode 100644 src/tests/test_addressbook.py

diff --git a/src/bitmessageqt/__init__.py b/src/bitmessageqt/__init__.py
index 34fee6d3..76290de5 100644
--- a/src/bitmessageqt/__init__.py
+++ b/src/bitmessageqt/__init__.py
@@ -36,6 +36,7 @@ from foldertree import (
 import settingsmixin
 import support
 from helper_sql import sqlQuery, sqlExecute, sqlExecuteChunked, sqlStoredProcedure
+import helper_addressbook
 import helper_search
 import l10n
 from utils import str_broadcast_subscribers, avatarize
@@ -2435,12 +2436,14 @@ class MyForm(settingsmixin.SMainWindow):
             ))
             return
 
-        self.addEntryToAddressBook(address, label)
-
-    def addEntryToAddressBook(self, address, label):
-        if shared.isAddressInMyAddressBook(address):
+        if address in BMConfigParser().addresses():
+            self.updateStatusBar(_translate(
+                "MainWindow",
+                "Error: You cannot add your own address in the address book."
+            ))
             return
-        sqlExecute('''INSERT INTO addressbook VALUES (?,?)''', label, address)
+
+        helper_addressbook.insert(label=label, address=address)
         self.rerenderMessagelistFromLabels()
         self.rerenderMessagelistToLabels()
         self.rerenderAddressBook()
diff --git a/src/helper_addressbook.py b/src/helper_addressbook.py
new file mode 100644
index 00000000..8e36b745
--- /dev/null
+++ b/src/helper_addressbook.py
@@ -0,0 +1,11 @@
+"""
+Insert value into addressbook
+"""
+
+from helper_sql import sqlExecute
+
+
+def insert(address, label):
+    """perform insert into addressbook"""
+
+    sqlExecute('''INSERT INTO addressbook VALUES (?,?)''', label, address)
diff --git a/src/tests/test_addressbook.py b/src/tests/test_addressbook.py
new file mode 100644
index 00000000..061af374
--- /dev/null
+++ b/src/tests/test_addressbook.py
@@ -0,0 +1,20 @@
+"""
+Tests for addresbook validation
+"""
+
+import unittest
+from pybitmessage.bmconfigparser import BMConfigParser
+
+
+class TestAddAddressBook(unittest.TestCase):
+    """this class is used for testting add address feature"""
+
+    def test_is_own_address_add_to_addressbook(self):
+        """Check the logic of TCPConnection.local"""
+        own_addresses = BMConfigParser().addresses()
+        if own_addresses:
+            address = own_addresses[0]
+        else:
+            address = 'BM-2cWiUsWMo4Po2UyNB3VyQdF3gxGrDX9gNm'
+
+        self.assertTrue(address not in own_addresses)