File: ofxconfig.py

package info (click to toggle)
python-ofxclient 1.3.8-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 224 kB
  • ctags: 156
  • sloc: python: 1,082; makefile: 4
file content (121 lines) | stat: -rw-r--r-- 4,825 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import unittest
import tempfile
import os
import os.path
import ofxclient.config
from ofxclient.config import OfxConfig
from ofxclient import Institution, CreditCardAccount
import StringIO



class OfxConfigTests(unittest.TestCase):

    def setUp(self):
        self.temp_file = tempfile.NamedTemporaryFile()

    def tearDown(self):
        self.temp_file.close()

    def testFileCreated(self):
        file_name = self.temp_file.name
        self.temp_file.close()

        self.assertFalse(os.path.exists(file_name))

        c = OfxConfig(file_name=file_name)
        self.assertTrue(os.path.exists(file_name))

        os.remove(file_name)

    def testAddAccount(self):
        c = OfxConfig(file_name=self.temp_file.name)

        i = Institution(id='1',org='org',url='url',username='user',password='pass')
        a = CreditCardAccount(institution=i,number='12345')

        c.add_account(a)
        self.assertEqual( len(c.accounts()), 1 )
        self.assertEqual( c.account(a.local_id()).local_id(), a.local_id() )

    def testLoadFromFile(self):
        c = OfxConfig(file_name=self.temp_file.name)
        i = Institution(id='1',org='org',url='url',username='user',password='pass')
        a = CreditCardAccount(institution=i,number='12345')
        c.add_account(a)
        c.save()

        c = OfxConfig(file_name=self.temp_file.name)
        got = c.account(a.local_id())
        self.assertEqual( len(c.accounts()), 1 )
        self.assertEqual( got.local_id(), a.local_id() )
        self.assertEqual( got.number, a.number )
        self.assertEqual( got.institution.local_id(), a.institution.local_id() )
        self.assertEqual( got.institution.id, a.institution.id )
        self.assertEqual( got.institution.org, a.institution.org )
        self.assertEqual( got.institution.url, a.institution.url )
        self.assertEqual( got.institution.username, a.institution.username )
        self.assertEqual( got.institution.password, a.institution.password )

    def testFieldsSecured(self):
        if not ofxclient.config.KEYRING_AVAILABLE:
            return

        c = OfxConfig(file_name=self.temp_file.name)

        i = Institution(id='1',org='org',url='url',username='user',password='pass')
        a = CreditCardAccount(institution=i,number='12345')
        c.add_account(a)

        self.assertTrue( c.parser.is_secure_option(a.local_id(),'institution.username') )
        self.assertTrue( c.parser.is_secure_option(a.local_id(),'institution.password') )

    def testFieldsRemainUnsecure(self):
        if not ofxclient.config.KEYRING_AVAILABLE:
            return

        c = OfxConfig(file_name=self.temp_file.name)
        i = Institution(id='1',org='org',url='url',username='user',password='pass')
        a = CreditCardAccount(institution=i,number='12345')
        c.add_account(a)

        # pretend the user put their password in there in the clear on purpose
        c.parser.remove_option(a.local_id(),'institution.password')
        c.parser.set(a.local_id(),'institution.password','pass')
        c.save()

        c = OfxConfig(file_name=self.temp_file.name)
        self.assertTrue( c.parser.is_secure_option(a.local_id(),'institution.username') )
        self.assertFalse( c.parser.is_secure_option(a.local_id(),'institution.password') )

    def testResecuredAfterEncryptAccount(self):
        if not ofxclient.config.KEYRING_AVAILABLE:
            return

        c = OfxConfig(file_name=self.temp_file.name)
        i = Institution(id='1',org='org',url='url',username='user',password='pass')
        a1 = CreditCardAccount(institution=i,number='12345')
        c.add_account(a1)
        a2 = CreditCardAccount(institution=i,number='67890')
        c.add_account(a2)

        # pretend the user put their password in there in the clear on purpose
        # to fix something... and then wants it to be resecured later on
        c.parser.remove_option(a1.local_id(),'institution.password')
        c.parser.set(a1.local_id(),'institution.password','pass')
        c.save()

        c = OfxConfig(file_name=self.temp_file.name)
        self.assertEqual( len(c.accounts()), 2 )
        self.assertEqual( len(c.encrypted_accounts()), 1 )
        self.assertEqual( len(c.unencrypted_accounts()), 1 )
        self.assertTrue( c.parser.is_secure_option(a1.local_id(),'institution.username') )
        self.assertFalse( c.parser.is_secure_option(a1.local_id(),'institution.password') )

        c.encrypt_account(a1.local_id())
        self.assertEqual( len(c.accounts()), 2 )
        self.assertEqual( len(c.encrypted_accounts()), 2 )
        self.assertEqual( len(c.unencrypted_accounts()), 0 )
        self.assertTrue( c.parser.is_secure_option(a1.local_id(),'institution.username') )
        self.assertTrue( c.parser.is_secure_option(a1.local_id(),'institution.password') )