Skip to content

Commit 07dba43

Browse files
committed
Add TestAuditEntry_Unmarshal
1 parent b0984dc commit 07dba43

File tree

1 file changed

+126
-2
lines changed

1 file changed

+126
-2
lines changed

github/orgs_audit_log_test.go

+126-2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package github
77

88
import (
99
"context"
10+
"encoding/json"
1011
"fmt"
1112
"net/http"
1213
"strings"
@@ -239,8 +240,6 @@ func TestAuditEntry_Marshal(t *testing.T) {
239240
t.Parallel()
240241
testJSONMarshal(t, &AuditEntry{}, "{}")
241242

242-
testJSONMarshal(t, &AuditEntry{AdditionalFields: map[string]interface{}{}}, "{}")
243-
244243
u := &AuditEntry{
245244
Action: Ptr("a"),
246245
Actor: Ptr("ac"),
@@ -437,3 +436,128 @@ func TestAuditEntry_Marshal(t *testing.T) {
437436

438437
testJSONMarshal(t, u, want)
439438
}
439+
440+
func TestAuditEntry_Unmarshal(t *testing.T) {
441+
t.Parallel()
442+
443+
// Test case 1: JSON with both defined fields and additional fields
444+
jsonData := `{
445+
"action": "org.update_member",
446+
"actor": "testuser",
447+
"actor_location": {
448+
"country_code": "US"
449+
},
450+
"created_at": "2021-03-07T00:35:08.000Z",
451+
"org": "testorg",
452+
"org_id": 12345,
453+
"user": "memberuser",
454+
"user_id": 67890,
455+
"custom_field": "custom_value",
456+
"another_field": 42,
457+
"nested_field": {
458+
"key": "value"
459+
},
460+
"array_field": ["item1", "item2"]
461+
}`
462+
463+
var entry AuditEntry
464+
err := json.Unmarshal([]byte(jsonData), &entry)
465+
if err != nil {
466+
t.Errorf("Error unmarshaling JSON: %v", err)
467+
}
468+
469+
// Check defined fields
470+
if *entry.Action != "org.update_member" {
471+
t.Errorf("Action = %v, want %v", *entry.Action, "org.update_member")
472+
}
473+
if *entry.Actor != "testuser" {
474+
t.Errorf("Actor = %v, want %v", *entry.Actor, "testuser")
475+
}
476+
if *entry.ActorLocation.CountryCode != "US" {
477+
t.Errorf("ActorLocation.CountryCode = %v, want %v", *entry.ActorLocation.CountryCode, "US")
478+
}
479+
if *entry.Org != "testorg" {
480+
t.Errorf("Org = %v, want %v", *entry.Org, "testorg")
481+
}
482+
if *entry.OrgID != 12345 {
483+
t.Errorf("OrgID = %v, want %v", *entry.OrgID, 12345)
484+
}
485+
if *entry.User != "memberuser" {
486+
t.Errorf("User = %v, want %v", *entry.User, "memberuser")
487+
}
488+
if *entry.UserID != 67890 {
489+
t.Errorf("UserID = %v, want %v", *entry.UserID, 67890)
490+
}
491+
492+
// Check additional fields
493+
if entry.AdditionalFields["custom_field"] != "custom_value" {
494+
t.Errorf("AdditionalFields[\"custom_field\"] = %v, want %v", entry.AdditionalFields["custom_field"], "custom_value")
495+
}
496+
if entry.AdditionalFields["another_field"] != float64(42) {
497+
t.Errorf("AdditionalFields[\"another_field\"] = %v, want %v", entry.AdditionalFields["another_field"], float64(42))
498+
}
499+
500+
// Check nested fields
501+
nestedField, ok := entry.AdditionalFields["nested_field"].(map[string]interface{})
502+
if !ok {
503+
t.Errorf("AdditionalFields[\"nested_field\"] is not a map")
504+
} else if nestedField["key"] != "value" {
505+
t.Errorf("AdditionalFields[\"nested_field\"][\"key\"] = %v, want %v", nestedField["key"], "value")
506+
}
507+
508+
// Check array fields
509+
arrayField, ok := entry.AdditionalFields["array_field"].([]interface{})
510+
if !ok {
511+
t.Errorf("AdditionalFields[\"array_field\"] is not an array")
512+
} else {
513+
if len(arrayField) != 2 {
514+
t.Errorf("len(AdditionalFields[\"array_field\"]) = %v, want %v", len(arrayField), 2)
515+
}
516+
if arrayField[0] != "item1" {
517+
t.Errorf("AdditionalFields[\"array_field\"][0] = %v, want %v", arrayField[0], "item1")
518+
}
519+
if arrayField[1] != "item2" {
520+
t.Errorf("AdditionalFields[\"array_field\"][1] = %v, want %v", arrayField[1], "item2")
521+
}
522+
}
523+
524+
// Test case 2: Empty JSON
525+
err = json.Unmarshal([]byte("{}"), &entry)
526+
if err != nil {
527+
t.Errorf("Error unmarshaling empty JSON: %v", err)
528+
}
529+
if entry.AdditionalFields != nil {
530+
t.Errorf("AdditionalFields = %v, want nil", entry.AdditionalFields)
531+
}
532+
533+
// Test case 3: JSON with only additional fields
534+
jsonData = `{
535+
"custom_field": "custom_value",
536+
"another_field": 42
537+
}`
538+
539+
err = json.Unmarshal([]byte(jsonData), &entry)
540+
if err != nil {
541+
t.Errorf("Error unmarshaling JSON with only additional fields: %v", err)
542+
}
543+
if entry.AdditionalFields["custom_field"] != "custom_value" {
544+
t.Errorf("AdditionalFields[\"custom_field\"] = %v, want %v", entry.AdditionalFields["custom_field"], "custom_value")
545+
}
546+
if entry.AdditionalFields["another_field"] != float64(42) {
547+
t.Errorf("AdditionalFields[\"another_field\"] = %v, want %v", entry.AdditionalFields["another_field"], float64(42))
548+
}
549+
550+
// Test case 4: Test that nil values in AdditionalFields are removed
551+
jsonData = `{
552+
"action": "org.update_member",
553+
"null_field": null
554+
}`
555+
556+
err = json.Unmarshal([]byte(jsonData), &entry)
557+
if err != nil {
558+
t.Errorf("Error unmarshaling JSON with null field: %v", err)
559+
}
560+
if _, exists := entry.AdditionalFields["null_field"]; exists {
561+
t.Errorf("AdditionalFields contains null_field, but it should have been removed")
562+
}
563+
}

0 commit comments

Comments
 (0)