secrit 0.0.4 → 0.0.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/secrit.rb +41 -10
  3. data/test/secrit_test.rb +45 -1
  4. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 70cd3930aa08c99cedd7b991a68e67e9136e9eecea9e37936d780ca5ea9ffb5a
4
- data.tar.gz: 40f8176c0130166a9297d30192defb9ef6d7127e226de16832b664a20e1eb915
3
+ metadata.gz: ebe7a9abd529893db98b6ea4169e629418e6328210f57a3c84b953f960c15301
4
+ data.tar.gz: bff251e6d9c4ebf773480d04d5b2b1a40a0895260bede6112260804b4fcee8d6
5
5
  SHA512:
6
- metadata.gz: da7895eab947c91fd9d3b5f9e25209b58861b077ea68332292142f79b31e34d43028316e8a6d3c0120246e07251b9d259c957c316d27d4ad69911086cfacb97f
7
- data.tar.gz: a21ae1183cc336b570831eb0dba023672c4bef71266714579c01acb6d8027dff6fd5b622620746eab4c935ad6696b9cef896162ca62b75db5f52e168996b27c9
6
+ metadata.gz: 454260a665033b13a4e2de64ccd9d49642678b25aca857e1b9b40f8c82c988b9900f1ba361dd7e7fa7cc94dcc35843993695d4aaa591f603617b019ed0f227ad
7
+ data.tar.gz: 4eecd853e9b0ced9a7029cfc81a1d0360662f7dcbc282112eed9c4608f862a9b6d9debd0a051d5efcc6102fe9001f9a19f818fc3093b6ea65cbb1581ca8b2a38
data/lib/secrit.rb CHANGED
@@ -1,20 +1,51 @@
1
1
  #!/usr/bin/env ruby
2
2
  require 'gpgme'
3
3
 
4
- PASSWORD_STORE_PATH = File.expand_path("~/.password-store/")
4
+ # Define an interface (or contract) for decryption
5
+ class Decryptor
6
+ def decrypt(file_path)
7
+ raise NotImplementedError
8
+ end
9
+ end
10
+
11
+ # Provide a default implementation
12
+ class GPGDecryptor < Decryptor
13
+ def decrypt(file_path)
14
+ crypto = GPGME::Crypto.new
15
+ decrypted_data = crypto.decrypt(File.open(file_path))
16
+ #decrypted_data.to_s.strip
17
+ decrypted_data
18
+ end
19
+ end
5
20
 
6
21
  class Secrit
7
- def self.get(entry_path)
8
- full_path = File.join(PASSWORD_STORE_PATH, "#{entry_path}.gpg")
22
+ attr_accessor :storage
9
23
 
10
- # Ensure the file exists
11
- raise "File not found: #{full_path}" unless File.exist?(full_path)
24
+ def self.get(entry_path, storage: nil)
25
+ new(
26
+ storage: storage
27
+ ).get(entry_path).to_s.strip
28
+ end
12
29
 
13
- # Decrypt the file
14
- crypto = GPGME::Crypto.new
15
- decrypted_data = crypto.decrypt(File.open(full_path))
30
+ # Allow optional injection of custom Decryptor
31
+ def initialize(decryptor: GPGDecryptor.new, storage: nil)
32
+ @decryptor = decryptor
33
+ @storage = storage || File.expand_path("~/.password-store/")
34
+ end
35
+
36
+ def get(entry_path)
37
+ full_path = construct_path(entry_path)
38
+ validate_file_existence(full_path)
39
+ @decryptor.decrypt(full_path)
40
+ end
16
41
 
17
- # Return the decrypted content
18
- decrypted_data.to_s.strip
42
+ private
43
+
44
+ def construct_path(entry_path)
45
+ File.join(storage, "#{entry_path}.gpg")
46
+ end
47
+
48
+ def validate_file_existence(full_path)
49
+ raise "File not found: #{full_path}" unless File.exist?(full_path)
19
50
  end
20
51
  end
data/test/secrit_test.rb CHANGED
@@ -4,6 +4,30 @@ require 'mocha/minitest'
4
4
  require_relative '../lib/secrit'
5
5
 
6
6
  class SecritTest < Minitest::Test
7
+ def with_encrypted_file(contents, key_path = 'test_entry')
8
+ Dir.mktmpdir do |temp_dir|
9
+ # Create a temporary GPG file with known content
10
+ test_file_path = File.join(temp_dir, "#{key_path}.gpg")
11
+ file = File.open(test_file_path, 'w+')
12
+
13
+ crypto = GPGME::Crypto.new
14
+ crypto.encrypt(contents, output: file)
15
+
16
+ yield(temp_dir)
17
+ end
18
+ end
19
+
20
+ def test_real_decryption
21
+ # Create a temporary GPG file with known content
22
+ key_path = 'foo_bar'
23
+
24
+ with_encrypted_file('test content', key_path) do |storage|
25
+ # Run the get method and check the result
26
+ result = Secrit.get(key_path, storage: storage)
27
+ assert_equal "test content", result
28
+ end
29
+ end
30
+
7
31
  def test_successful_decryption
8
32
  # Mocking the file existence check
9
33
  File.stubs(:exist?).returns(true)
@@ -21,12 +45,32 @@ class SecritTest < Minitest::Test
21
45
  assert_equal 'decrypted content', result
22
46
  end
23
47
 
48
+ def test_successful_decryption_object
49
+ entry_path = "valid_entry_path"
50
+ expected_decrypted_content = "secret content"
51
+
52
+ # Create a mock decryptor
53
+ mock_decryptor = Minitest::Mock.new
54
+ mock_decryptor.expect(:decrypt, expected_decrypted_content, [String])
55
+ File.stubs(:exist?).returns(true)
56
+
57
+ # Create an instance of Secrit with the mock decryptor
58
+ secrit = Secrit.new(decryptor: mock_decryptor)
59
+
60
+ # Test the get method
61
+ decrypted_content = secrit.get(entry_path)
62
+ assert_equal expected_decrypted_content, decrypted_content
63
+
64
+ # Verify that the mock expectations were met
65
+ mock_decryptor.verify
66
+ end
67
+
24
68
  def test_file_not_found
25
69
  # Mocking the file existence check to return false
26
70
  File.stubs(:exist?).returns(false)
27
71
 
28
72
  # Testing the get method to ensure it raises the correct error
29
- assert_raises(RuntimeError, "File not found: #{File.join(PASSWORD_STORE_PATH, 'non_existent_file.gpg')}") do
73
+ assert_raises(RuntimeError, "File not found: #{File.join(Secrit.new.storage, 'non_existent_file.gpg')}") do
30
74
  Secrit.get('non_existent_file')
31
75
  end
32
76
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: secrit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Simpthy