Skip to content

Commit

Permalink
Adjust golden files to cover broker cache structure
Browse files Browse the repository at this point in the history
  • Loading branch information
denisonbarbosa committed May 28, 2024
1 parent 1bd8b66 commit 3591378
Show file tree
Hide file tree
Showing 44 changed files with 59 additions and 34 deletions.
73 changes: 45 additions & 28 deletions internal/broker/broker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ubuntu/oidc-broker/internal/providers/group"
"github.com/ubuntu/oidc-broker/internal/testutils"
"golang.org/x/oauth2"
"gopkg.in/yaml.v3"
)

var defaultProvider *httptest.Server
Expand Down Expand Up @@ -360,11 +361,14 @@ func TestIsAuthenticated(t *testing.T) {
require.NoError(t, err, "Setup: SaveToken should not have returned an error")
}

var readOnlyCacheCleanup, readOnlyTokenCleanup func()
if tc.readOnlyCacheDir {
if tc.preexistentToken != "" {
testutils.MakeReadOnly(t, b.TokenPathForSession(sessionID))
readOnlyTokenCleanup = testutils.MakeReadOnly(t, b.TokenPathForSession(sessionID))
t.Cleanup(readOnlyTokenCleanup)
}
testutils.MakeReadOnly(t, b.CachePath())
readOnlyCacheCleanup = testutils.MakeReadOnly(t, b.CachePath())
t.Cleanup(readOnlyCacheCleanup)
}

switch tc.firstChallenge {
Expand Down Expand Up @@ -414,46 +418,59 @@ func TestIsAuthenticated(t *testing.T) {
errStr := strings.ReplaceAll(fmt.Sprintf("%v", err), sessionID, "SESSION_ID")

got := isAuthenticatedResponse{Access: access, Data: data, Err: errStr}
goldenPath := testutils.GoldenPath(t) + ".first_call"

want := testutils.LoadWithUpdateFromGoldenYAML(t, got, testutils.WithGoldenPath(goldenPath))
require.Equal(t, want, got, "IsAuthenticated should have returned the expected value")
out, err := yaml.Marshal(got)
require.NoError(t, err, "Failed to marshal first response")

err = os.WriteFile(filepath.Join(outDir, "first_call"), out, 0600)
require.NoError(t, err, "Failed to write first response")
}()

if !tc.dontWaitForFirstCall {
<-firstCallDone
}
if !tc.wantSecondCall {
return
}

// Give some time for the first call
time.Sleep(10 * time.Millisecond)
if tc.wantSecondCall {
// Give some time for the first call
time.Sleep(10 * time.Millisecond)

secondAuthData := `{"challenge":"` + encryptChallenge(t, "passwordpassword", key) + `"}`
if tc.invalidAuthData {
secondAuthData = "invalid json"
}
secondAuthData := `{"challenge":"` + encryptChallenge(t, "passwordpassword", key) + `"}`
if tc.invalidAuthData {
secondAuthData = "invalid json"
}

secondCallDone := make(chan struct{})
go func() {
defer close(secondCallDone)
secondCallDone := make(chan struct{})
go func() {
defer close(secondCallDone)

updateAuthModes(t, b, sessionID, "newpassword")
updateAuthModes(t, b, sessionID, "newpassword")

access, data, err := b.IsAuthenticated(sessionID, secondAuthData)
access, data, err := b.IsAuthenticated(sessionID, secondAuthData)

// Redact variant session id from the response
data = strings.ReplaceAll(data, sessionID, "SESSION_ID")
errStr := strings.ReplaceAll(fmt.Sprintf("%v", err), sessionID, "SESSION_ID")
// Redact variant session id from the response
data = strings.ReplaceAll(data, sessionID, "SESSION_ID")
errStr := strings.ReplaceAll(fmt.Sprintf("%v", err), sessionID, "SESSION_ID")

got := isAuthenticatedResponse{Access: access, Data: data, Err: errStr}
goldenPath := testutils.GoldenPath(t) + ".second_call"
got := isAuthenticatedResponse{Access: access, Data: data, Err: errStr}
out, err := yaml.Marshal(got)
require.NoError(t, err, "Failed to marshal second response")

want := testutils.LoadWithUpdateFromGoldenYAML(t, got, testutils.WithGoldenPath(goldenPath))
require.Equal(t, want, got, "IsAuthenticated should have returned the expected value")
}()
err = os.WriteFile(filepath.Join(outDir, "second_call"), out, 0600)
require.NoError(t, err, "Failed to write second response")
}()
<-secondCallDone
}
<-firstCallDone
<-secondCallDone

// We need to restore some permissions in order to save the golden files.
if tc.readOnlyCacheDir {
readOnlyCacheCleanup()
if tc.preexistentToken != "" {
readOnlyTokenCleanup()
}
}

testutils.CompareTreesWithFiltering(t, outDir, testutils.GoldenPath(t), testutils.Update())
})
}
}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
password
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
password
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
access: granted
data: |-
{"userinfo": {
"name": "saved[email protected]",
"name": "test[email protected]",
"uuid": "saved-user-id",
"gecos": "saved-user",
"dir": "/home/saved[email protected]",
"dir": "/home/test[email protected]",
"shell": "/usr/bin/bash",
"groups": [{"name": "saved-remote-group", "gid": "12345"}, {"name": "saved-local-group", "gid": ""}]
}}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
qrcode
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
access: ""
access: denied
data: ""
err: authentication data is not a valid json value
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
access: ""
access: denied
data: ""
err: IsAuthenticated already running for session "SESSION_ID"
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
�Vۇ�:�qBy?����pa�y-�Q/'W��;j^� ���I_��HN"˼���f��I=�~F�Q�{j)��8�7o��� �5�G[��j�$�ӑ�Ҕu=�����@�?�/Y�sE����+��M�����k�d�ϛ��V�yZ��;�~[�/�H&%Q�28T�W����(Sʤ����V�����qz� �yg^�4#�L�":"o�@7��9%�I`n����^>�"��>���o����T�]��n�]j�,���ٹ{G�:���_0�L>ǿ=�UR���&�ŀ蜅Ј����=�V�����=�rO�2��� �'�q� �:���[Q�'���I���%��h�P-I�ir�ϳے�/�I��y��!O�܇�ґ�����$��2�Co��;�g kf�S�W �Q��n��R�(���>��Jr]#���Z�S?�ٹ�ύ�A� G�T��l3-��/Q�5c���g����2w`sC)0��؇z�1 )�r� ���G
{۟�|�qc�e90�A[*��E
Expand Down
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
V4�-)�{�1�7h��:ko��T{��\����%�0����hT�?�Y�P��7��Z��%�t��5���M[���▸j{Rn!��'��9U� X��sX�7�
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
password
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
access: granted
data: |-
{"userinfo": {
"name": "saved[email protected]",
"name": "test[email protected]",
"uuid": "saved-user-id",
"gecos": "saved-user",
"dir": "/home/saved[email protected]",
"dir": "/home/test[email protected]",
"shell": "/usr/bin/bash",
"groups": [ {"name": "remote-group", "ugid": "12345"}, {"name": "linux-local-group", "ugid": ""} ]
}}
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
qrcode

0 comments on commit 3591378

Please sign in to comment.