-
Notifications
You must be signed in to change notification settings - Fork 2.2k
feat: Add JSON marshal tests for RepositoryRulesetRules & PublicKey
#3937
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
e5f03fc
3e83711
807e3b5
02e82f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ | |
| package github | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "strings" | ||
|
|
@@ -420,3 +421,47 @@ func TestAuditEntry_Marshal(t *testing.T) { | |
|
|
||
| testJSONMarshal(t, u, want) | ||
| } | ||
|
|
||
| func TestAuditEntry_UnmarshalJSON_NoAdditionalFields(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| var entry AuditEntry | ||
| payload := []byte(`{"action":"login","actor":"octo","token_id":10,"unknown":null}`) | ||
|
|
||
| if err := json.Unmarshal(payload, &entry); err != nil { | ||
| t.Fatalf("json.Unmarshal returned error: %v", err) | ||
| } | ||
|
|
||
| if entry.Action == nil || *entry.Action != "login" { | ||
| t.Fatalf("Action mismatch, got: %v", entry.Action) | ||
| } | ||
|
|
||
| if entry.Actor == nil || *entry.Actor != "octo" { | ||
| t.Fatalf("Actor mismatch, got: %v", entry.Actor) | ||
| } | ||
|
|
||
| if entry.TokenID == nil || *entry.TokenID != int64(10) { | ||
| t.Fatalf("TokenID mismatch, got: %v", entry.TokenID) | ||
| } | ||
|
|
||
| if entry.AdditionalFields != nil { | ||
| t.Fatalf("AdditionalFields should be nil, got: %#v", entry.AdditionalFields) | ||
| } | ||
| } | ||
|
|
||
| func TestAuditEntry_MarshalJSON_FieldCollision(t *testing.T) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test should be placed after the |
||
| t.Parallel() | ||
|
|
||
| entry := &AuditEntry{ | ||
| Action: Ptr("login"), | ||
| AdditionalFields: map[string]any{ | ||
| "action": "override", | ||
| }, | ||
| } | ||
|
|
||
| if _, err := entry.MarshalJSON(); err == nil { | ||
| t.Fatal("AuditEntry.MarshalJSON expected error for field collision, got nil") | ||
| } else if !strings.Contains(err.Error(), "unexpected field") { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| // Copyright 2020 The go-github AUTHORS. All rights reserved. | ||
| // | ||
| // Use of this source code is governed by a BSD-style | ||
| // license that can be found in the LICENSE file. | ||
|
|
||
| package github | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestRepositoryRulesetRules_MarshalJSON_Empty(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| got, err := json.Marshal(&RepositoryRulesetRules{}) | ||
| if err != nil { | ||
| t.Fatalf("json.Marshal returned error: %v", err) | ||
| } | ||
|
|
||
| if string(got) != "[]" { | ||
| t.Fatalf("expected empty array for no rules, got %s", got) | ||
| } | ||
| } | ||
|
|
||
| func TestMarshalRepositoryRulesetRule_UpdateTypeValidation(t *testing.T) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding a test for the unexported function |
||
| t.Parallel() | ||
|
|
||
| if _, err := marshalRepositoryRulesetRule(RulesetRuleTypeUpdate, &EmptyRuleParameters{}); err == nil { | ||
| t.Fatal("expected type validation error, got nil") | ||
| } else if !strings.Contains(err.Error(), "UpdateRuleParameters") { | ||
| t.Fatalf("unexpected error: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestMarshalRepositoryRulesetRule_UpdateNoParams(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| bytes, err := marshalRepositoryRulesetRule(RulesetRuleTypeUpdate, (*UpdateRuleParameters)(nil)) | ||
| if err != nil { | ||
| t.Fatalf("marshalRepositoryRulesetRule returned error: %v", err) | ||
| } | ||
|
|
||
| if string(bytes) != `{"type":"update"}` { | ||
| t.Fatalf("marshalRepositoryRulesetRule expected type-only payload, got %s", bytes) | ||
| } | ||
| } | ||
|
|
||
| func TestRepositoryRulesetRules_UnmarshalJSON(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| payload := `[{"type":"creation"},{"type":"required_deployments","parameters":{"required_deployment_environments":["prod"]}}]` | ||
| var rules RepositoryRulesetRules | ||
|
|
||
| if err := json.Unmarshal([]byte(payload), &rules); err != nil { | ||
| t.Fatalf("json.Unmarshal returned error: %v", err) | ||
| } | ||
|
|
||
| if rules.Creation == nil { | ||
| t.Fatalf("Creation rule was not populated: %#v", rules.Creation) | ||
| } | ||
|
|
||
| if rules.RequiredDeployments == nil || len(rules.RequiredDeployments.RequiredDeploymentEnvironments) != 1 || rules.RequiredDeployments.RequiredDeploymentEnvironments[0] != "prod" { | ||
| t.Fatalf("RequiredDeployments not populated as expected: %#v", rules.RequiredDeployments) | ||
| } | ||
| } | ||
|
|
||
| func TestRepositoryRule_UnmarshalJSON(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| var creation RepositoryRule | ||
| if err := json.Unmarshal([]byte(`{"type":"creation"}`), &creation); err != nil { | ||
| t.Fatalf("json.Unmarshal returned error: %v", err) | ||
| } | ||
|
|
||
| if creation.Type != RulesetRuleTypeCreation { | ||
| t.Fatalf("Type mismatch, got %v", creation.Type) | ||
| } | ||
|
|
||
| if creation.Parameters != nil { | ||
| t.Fatalf("creation rule should not carry parameters, got %#v", creation.Parameters) | ||
| } | ||
|
|
||
| var update RepositoryRule | ||
| if err := json.Unmarshal([]byte(`{"type":"update","parameters":{"update_allows_fetch_and_merge":true}}`), &update); err != nil { | ||
| t.Fatalf("json.Unmarshal returned error: %v", err) | ||
| } | ||
|
|
||
| params, ok := update.Parameters.(*UpdateRuleParameters) | ||
| if !ok || params == nil { | ||
| t.Fatalf("update parameters not decoded: %#v", update.Parameters) | ||
| } | ||
|
|
||
| if !params.UpdateAllowsFetchAndMerge { | ||
| t.Fatalf("UpdateAllowsFetchAndMerge should be true, got %#v", params) | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please take a look at the other tests in this file and consider rewriting the new ones to follow the same code style for better consistency.