@@ -18,6 +18,7 @@ import (
1818
1919var (
2020 testUsername = "gitlab-shell"
21+ testKeyID = 123
2122 testRepo = "gitlab-org/gitlab-shell"
2223 testPackfileWants int64 = 100
2324 testPackfileHaves int64 = 100
@@ -28,26 +29,57 @@ var (
2829)
2930
3031func TestAudit (t * testing.T ) {
31- client := setup (t , http .StatusOK )
32+ tests := []struct {
33+ name string
34+ keyID int
35+ expectKeyID bool
36+ }{
37+ {
38+ name : "with key_id" ,
39+ keyID : testKeyID ,
40+ expectKeyID : true ,
41+ },
42+ {
43+ name : "without key_id" ,
44+ keyID : 0 ,
45+ expectKeyID : false ,
46+ },
47+ }
3248
33- err := client .Audit (context .Background (), testUsername , testArgs , testRepo , & pb.PackfileNegotiationStatistics {
34- Wants : testPackfileWants ,
35- Haves : testPackfileHaves ,
36- })
37- require .NoError (t , err )
49+ for _ , tt := range tests {
50+ t .Run (tt .name , func (t * testing.T ) {
51+ client := setup (t , http .StatusOK , tt .keyID , tt .expectKeyID )
52+
53+ err := client .Audit (context .Background (), AuditParams {
54+ Username : testUsername ,
55+ KeyID : tt .keyID ,
56+ Repo : testRepo ,
57+ PackfileStats : & pb.PackfileNegotiationStatistics {
58+ Wants : testPackfileWants ,
59+ Haves : testPackfileHaves ,
60+ },
61+ }, testArgs )
62+ require .NoError (t , err )
63+ })
64+ }
3865}
3966
4067func TestAuditFailed (t * testing.T ) {
41- client := setup (t , http .StatusBadRequest )
68+ client := setup (t , http .StatusBadRequest , testKeyID , true )
4269
43- err := client .Audit (context .Background (), testUsername , testArgs , testRepo , & pb.PackfileNegotiationStatistics {
44- Wants : testPackfileWants ,
45- Haves : testPackfileHaves ,
46- })
70+ err := client .Audit (context .Background (), AuditParams {
71+ Username : testUsername ,
72+ KeyID : testKeyID ,
73+ Repo : testRepo ,
74+ PackfileStats : & pb.PackfileNegotiationStatistics {
75+ Wants : testPackfileWants ,
76+ Haves : testPackfileHaves ,
77+ },
78+ }, testArgs )
4779 require .Error (t , err )
4880}
4981
50- func setup (t * testing.T , responseStatus int ) * Client {
82+ func setup (t * testing.T , responseStatus int , keyID int , expectKeyID bool ) * Client {
5183 requests := []testserver.TestRequestHandler {
5284 {
5385 Path : uri ,
@@ -56,9 +88,20 @@ func setup(t *testing.T, responseStatus int) *Client {
5688 assert .NoError (t , err )
5789 defer r .Body .Close ()
5890
91+ // Check if key_id is present/absent in raw JSON
92+ var rawJSON map [string ]interface {}
93+ assert .NoError (t , json .Unmarshal (body , & rawJSON ))
94+ _ , hasKeyID := rawJSON ["key_id" ]
95+ if expectKeyID {
96+ assert .True (t , hasKeyID , "key_id should be present in JSON" )
97+ } else {
98+ assert .False (t , hasKeyID , "key_id should not be present in JSON" )
99+ }
100+
59101 var request * Request
60102 assert .NoError (t , json .Unmarshal (body , & request ))
61103 assert .Equal (t , testUsername , request .Username )
104+ assert .Equal (t , keyID , request .KeyID )
62105 assert .Equal (t , testArgs .Env .RemoteAddr , request .CheckIP )
63106 assert .Equal (t , testArgs .CommandType , request .Action )
64107 assert .Equal (t , testRepo , request .Repo )
0 commit comments