-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add EncryptPII() Signed-off-by: Raphaël Pinson <[email protected]> * Add EncryptUserPII() Signed-off-by: Raphaël Pinson <[email protected]> --------- Signed-off-by: Raphaël Pinson <[email protected]>
- Loading branch information
Showing
2 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,11 @@ | |
package instruqt | ||
|
||
import ( | ||
"crypto/rand" | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"net/url" | ||
"testing" | ||
|
||
"github.com/shurcooL/graphql" | ||
|
@@ -40,3 +45,71 @@ func TestGetTPGPublicKey(t *testing.T) { | |
assert.Equal(t, expectedPublicKey, publicKey) | ||
mockClient.AssertExpectations(t) | ||
} | ||
|
||
func TestEncryptPII(t *testing.T) { | ||
// Generate a temporary RSA key pair for testing | ||
privateKey, err := rsa.GenerateKey(rand.Reader, 2048) | ||
assert.NoError(t, err) | ||
|
||
// Extract the public key and encode it in PEM format | ||
publicKeyBytes, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey) | ||
assert.NoError(t, err) | ||
publicKeyPEM := pem.EncodeToMemory(&pem.Block{ | ||
Type: "RSA PUBLIC KEY", | ||
Bytes: publicKeyBytes, | ||
}) | ||
|
||
// Create the mock client | ||
mockClient := new(MockGraphQLClient) | ||
client := &Client{ | ||
GraphQLClient: mockClient, | ||
} | ||
mockClient.On("Query", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) { | ||
query := args.Get(1).(*teamQuery) | ||
query.Team.TPGPublicKey = graphql.String(publicKeyPEM) | ||
}).Return(nil) | ||
|
||
// Create the PII data to be encrypted | ||
data := url.Values{} | ||
data.Set("email", "[email protected]") | ||
data.Set("first_name", "John") | ||
data.Set("last_name", "Doe") | ||
|
||
// Call the EncryptPII function | ||
encryptedPII, err := client.EncryptPII(data.Encode()) | ||
assert.NoError(t, err) | ||
|
||
// Ensure the encrypted PII is a non-empty string | ||
assert.NotEmpty(t, encryptedPII) | ||
} | ||
|
||
func TestEncryptUserPII(t *testing.T) { | ||
// Generate a temporary RSA key pair for testing | ||
privateKey, err := rsa.GenerateKey(rand.Reader, 2048) | ||
assert.NoError(t, err) | ||
|
||
// Extract the public key and encode it in PEM format | ||
publicKeyBytes, err := x509.MarshalPKIXPublicKey(&privateKey.PublicKey) | ||
assert.NoError(t, err) | ||
publicKeyPEM := pem.EncodeToMemory(&pem.Block{ | ||
Type: "RSA PUBLIC KEY", | ||
Bytes: publicKeyBytes, | ||
}) | ||
|
||
// Create the mock client | ||
mockClient := new(MockGraphQLClient) | ||
client := &Client{ | ||
GraphQLClient: mockClient, | ||
} | ||
mockClient.On("Query", mock.Anything, mock.Anything, mock.Anything).Run(func(args mock.Arguments) { | ||
query := args.Get(1).(*teamQuery) | ||
query.Team.TPGPublicKey = graphql.String(publicKeyPEM) | ||
}).Return(nil) | ||
|
||
// Call the EncryptUserPII function | ||
encryptedPII, err := client.EncryptUserPII("John", "Doe", "[email protected]") | ||
assert.NoError(t, err) | ||
|
||
// Ensure the encrypted PII is a non-empty string | ||
assert.NotEmpty(t, encryptedPII) | ||
} |