| 1 | from elixir import * |
|---|
| 2 | from elixir.ext.encrypted import acts_as_encrypted |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | def setup(): |
|---|
| 6 | global Person, Pet |
|---|
| 7 | |
|---|
| 8 | class Person(Entity): |
|---|
| 9 | name = Field(String(50)) |
|---|
| 10 | password = Field(String(40)) |
|---|
| 11 | ssn = Field(String(40)) |
|---|
| 12 | pets = OneToMany('Pet') |
|---|
| 13 | acts_as_encrypted(for_fields=['password', 'ssn'], with_secret='secret') |
|---|
| 14 | |
|---|
| 15 | class Pet(Entity): |
|---|
| 16 | name = Field(String(50)) |
|---|
| 17 | codename = Field(String(50)) |
|---|
| 18 | acts_as_encrypted(for_fields=['codename'], with_secret='secret2') |
|---|
| 19 | owner = ManyToOne('Person') |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | metadata.bind = 'sqlite://' |
|---|
| 23 | setup_all() |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | def teardown(): |
|---|
| 27 | cleanup_all() |
|---|
| 28 | |
|---|
| 29 | |
|---|
| 30 | class TestEncryption(object): |
|---|
| 31 | def setup(self): |
|---|
| 32 | create_all() |
|---|
| 33 | |
|---|
| 34 | def teardown(self): |
|---|
| 35 | drop_all() |
|---|
| 36 | session.close() |
|---|
| 37 | |
|---|
| 38 | def test_encryption(self): |
|---|
| 39 | jonathan = Person( |
|---|
| 40 | name='Jonathan LaCour', |
|---|
| 41 | password='s3cr3tw0RD', |
|---|
| 42 | ssn='123-45-6789' |
|---|
| 43 | ) |
|---|
| 44 | winston = Pet( |
|---|
| 45 | name='Winston', |
|---|
| 46 | codename='Pug/Shih-Tzu Mix' |
|---|
| 47 | ) |
|---|
| 48 | nelson = Pet( |
|---|
| 49 | name='Nelson', |
|---|
| 50 | codename='Pug' |
|---|
| 51 | ) |
|---|
| 52 | jonathan.pets = [winston, nelson] |
|---|
| 53 | |
|---|
| 54 | session.commit() |
|---|
| 55 | session.clear() |
|---|
| 56 | |
|---|
| 57 | p = Person.get_by(name='Jonathan LaCour') |
|---|
| 58 | assert p.password == 's3cr3tw0RD' |
|---|
| 59 | assert p.ssn == '123-45-6789' |
|---|
| 60 | assert p.pets[0].name == 'Winston' |
|---|
| 61 | assert p.pets[0].codename == 'Pug/Shih-Tzu Mix' |
|---|
| 62 | assert p.pets[1].name == 'Nelson' |
|---|
| 63 | assert p.pets[1].codename == 'Pug' |
|---|
| 64 | |
|---|
| 65 | p.password = 'N3wpAzzw0rd' |
|---|
| 66 | |
|---|
| 67 | session.commit() |
|---|
| 68 | session.clear() |
|---|
| 69 | |
|---|
| 70 | p = Person.get_by(name='Jonathan LaCour') |
|---|
| 71 | assert p.password == 'N3wpAzzw0rd' |
|---|
| 72 | |
|---|
| 73 | def test_two_consecutive_updates(self): |
|---|
| 74 | jonathan = Person( |
|---|
| 75 | name='Jonathan LaCour', |
|---|
| 76 | password='s3cr3tw0RD', |
|---|
| 77 | ssn='123-45-6789' |
|---|
| 78 | ) |
|---|
| 79 | session.commit() |
|---|
| 80 | session.clear() |
|---|
| 81 | |
|---|
| 82 | p = Person.get_by(name='Jonathan LaCour') |
|---|
| 83 | assert p.password == 's3cr3tw0RD' |
|---|
| 84 | p.name = 'JONATHAN LACOUR' |
|---|
| 85 | session.flush() |
|---|
| 86 | |
|---|
| 87 | assert p.password == 'r\\x9d\\xa8\\xb4\\x8d|\\xffp\\xf5\\x0e' |
|---|
| 88 | |
|---|
| 89 | p.name = 'Jonathan LaCour' |
|---|
| 90 | session.flush() |
|---|
| 91 | |
|---|
| 92 | # check that it is not further encrypted |
|---|
| 93 | assert p.password == 'r\\x9d\\xa8\\xb4\\x8d|\\xffp\\xf5\\x0e' |
|---|
| 94 | |
|---|
| 95 | session.commit() |
|---|
| 96 | session.clear() |
|---|
| 97 | |
|---|
| 98 | p = Person.get_by(name='Jonathan LaCour') |
|---|
| 99 | assert p.password == 's3cr3tw0RD' |
|---|