44package cmd_test
55
66import (
7+ "bytes"
78 "context"
89 "encoding/json"
910 "testing"
1011
12+ "github.com/tidwall/sjson"
13+
1114 "github.com/stretchr/testify/assert"
1215 "github.com/stretchr/testify/require"
1316 "github.com/tidwall/gjson"
@@ -25,11 +28,11 @@ func TestUpdateClient(t *testing.T) {
2528 original := createClient (t , reg , nil )
2629 t .Run ("case=creates successfully" , func (t * testing.T ) {
2730 actual := gjson .Parse (cmdx .ExecNoErr (t , c , "--grant-type" , "implicit" , original .GetID ()))
28- expected , err := reg .ClientManager ().GetClient (ctx , actual .Get ("client_id" ).String () )
31+ expected , err := reg .ClientManager ().GetClient (ctx , actual .Get ("client_id" ).Str )
2932 require .NoError (t , err )
3033
31- assert .Equal (t , expected .GetID (), actual .Get ("client_id" ).String () )
32- assert .Equal (t , "implicit" , actual .Get ("grant_types" ).Array ()[0 ].String () )
34+ assert .Equal (t , expected .GetID (), actual .Get ("client_id" ).Str )
35+ assert .Equal (t , "implicit" , actual .Get ("grant_types" ).Array ()[0 ].Str )
3336 snapshotx .SnapshotT (t , json .RawMessage (actual .Raw ), snapshotExcludedClientFields ... )
3437 })
3538
@@ -39,9 +42,48 @@ func TestUpdateClient(t *testing.T) {
3942 "--secret" , "some-userset-secret" ,
4043 "--pgp-key" , base64EncodedPGPPublicKey (t ),
4144 ))
42- assert .NotEmpty (t , actual .Get ("client_id" ).String ())
43- assert .NotEmpty (t , actual .Get ("client_secret" ).String ())
45+ assert .Equal (t , original .ID , actual .Get ("client_id" ).Str )
46+ assert .NotEmpty (t , actual .Get ("client_secret" ).Str )
47+ assert .NotEqual (t , original .Secret , actual .Get ("client_secret" ).Str )
4448
4549 snapshotx .SnapshotT (t , json .RawMessage (actual .Raw ), snapshotExcludedClientFields ... )
4650 })
51+
52+ t .Run ("case=updates from file" , func (t * testing.T ) {
53+ original , err := reg .ClientManager ().GetConcreteClient (ctx , original .GetID ())
54+ require .NoError (t , err )
55+
56+ raw , err := json .Marshal (original )
57+ require .NoError (t , err )
58+
59+ t .Run ("file=stdin" , func (t * testing.T ) {
60+ raw , err = sjson .SetBytes (raw , "client_name" , "updated through file stdin" )
61+ require .NoError (t , err )
62+
63+ stdout , stderr , err := cmdx .Exec (t , c , bytes .NewReader (raw ), original .GetID (), "--file" , "-" )
64+ require .NoError (t , err , stderr )
65+
66+ actual := gjson .Parse (stdout )
67+ assert .Equal (t , original .ID , actual .Get ("client_id" ).Str )
68+ assert .Equal (t , "updated through file stdin" , actual .Get ("client_name" ).Str )
69+
70+ snapshotx .SnapshotT (t , json .RawMessage (actual .Raw ), snapshotExcludedClientFields ... )
71+ })
72+
73+ t .Run ("file=from disk" , func (t * testing.T ) {
74+ raw , err = sjson .SetBytes (raw , "client_name" , "updated through file from disk" )
75+ require .NoError (t , err )
76+
77+ fn := writeTempFile (t , json .RawMessage (raw ))
78+
79+ stdout , stderr , err := cmdx .Exec (t , c , nil , original .GetID (), "--file" , fn )
80+ require .NoError (t , err , stderr )
81+
82+ actual := gjson .Parse (stdout )
83+ assert .Equal (t , original .ID , actual .Get ("client_id" ).Str )
84+ assert .Equal (t , "updated through file from disk" , actual .Get ("client_name" ).Str )
85+
86+ snapshotx .SnapshotT (t , json .RawMessage (actual .Raw ), snapshotExcludedClientFields ... )
87+ })
88+ })
4789}
0 commit comments