File: test_state.py

package info (click to toggle)
pius 3.0.0-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 432 kB
  • sloc: python: 2,963; perl: 178; makefile: 2; sh: 1
file content (79 lines) | stat: -rwxr-xr-x 2,704 bytes parent folder | download | duplicates (3)
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
#!/usr/bin/env python3

# py3 explicitly for mock_open and friends
#

import unittest
from unittest.mock import patch, mock_open
from libpius.state import SignState

class TestFileConversion(unittest.TestCase):
  kSAMPLE = {
      'one': {'OUTBOUND': 'SIGNED', 'INBOUND': None},
      'two': {'OUTBOUND': 'SIGNED', 'INBOUND': None},
      'three': {'OUTBOUND': 'SIGNED', 'INBOUND': None},
  }

  def setUp(self):
    self.state = SignState()


  def test_convert_from_v1(self):
    self.assertEqual(
        self.state.convert_from_v1("one\ntwo\nthree"),
        self.kSAMPLE,
    )

  def test_convert_from_v2(self):
    self.assertEqual(
        self.state.convert_from_v2({
          'one': 'SIGNED',
          'two': 'SIGNED',
          'three': 'SIGNED',
          }),
        self.kSAMPLE,
    )

  @patch('libpius.state.SignState.write_file', create=True)
  def test_read_v1_writes_v3(self, write_file):
    data = "one\ntwo\nthree"
    new = {'four': {'OUTBOUND': 'SIGNED', 'INBOUND': None}}
    with patch('libpius.state.open', mock_open(read_data=data), create=True) as m:
        with patch('libpius.state.os.path.exists', return_value=True) as p:
            self.state.store_signed_keys(new)
    temp = self.kSAMPLE.copy()
    temp.update(new)
    temp.update(SignState.kFILE_METADATA)
    write_file.assert_called_once_with(temp)

  @patch('libpius.state.SignState.write_file', create=True)
  def test_read_v2_writes_v3(self, write_file):
    data = '{"one":"SIGNED","two":"SIGNED","three":"SIGNED"}'
    new = {'four': {'OUTBOUND': 'SIGNED', 'INBOUND': None}}
    with patch('libpius.state.open', mock_open(read_data=data)) as m:
        with patch('libpius.state.os.path.exists', return_value=True) as p:
            self.state.store_signed_keys(new)
    temp = self.kSAMPLE.copy()
    temp.update(new)
    temp.update(SignState.kFILE_METADATA)
    write_file.assert_called_once_with(temp)

  @patch('libpius.state.SignState.write_file', create=True)
  def test_read_empty_writes_v3_with_new_data(self, write_file):
    data = '{}'
    new = {'four': {'OUTBOUND': 'SIGNED', 'INBOUND': None}}
    with patch('libpius.state.open', mock_open(read_data=data)) as m:
      self.state.store_signed_keys(new)
    temp = new.copy()
    temp.update(SignState.kFILE_METADATA)
    write_file.assert_called_once_with(temp)

  @patch('libpius.state.SignState.write_file', create=True)
  def test_read_empty_writes_v3_with_no_new_data(self, write_file):
    data = '{}'
    new = {}
    with patch('libpius.state.open', mock_open(read_data=data)) as m:
      self.state.store_signed_keys(new)
    temp = new.copy()
    temp.update(SignState.kFILE_METADATA)
    write_file.assert_called_once_with(temp)