github.com/google/go-github/v66@v66.0.0/github/event_test.go (about)

     1  // Copyright 2020 The go-github AUTHORS. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style
     4  // license that can be found in the LICENSE file.
     5  
     6  package github
     7  
     8  import (
     9  	"encoding/json"
    10  	"testing"
    11  )
    12  
    13  func TestPayload_Panic(t *testing.T) {
    14  	t.Parallel()
    15  	defer func() {
    16  		if r := recover(); r == nil {
    17  			t.Errorf("Payload did not panic but should have")
    18  		}
    19  	}()
    20  
    21  	name := "UserEvent"
    22  	body := json.RawMessage("[") // bogus JSON
    23  	e := &Event{Type: &name, RawPayload: &body}
    24  	e.Payload()
    25  }
    26  
    27  func TestPayload_NoPanic(t *testing.T) {
    28  	t.Parallel()
    29  	name := "UserEvent"
    30  	body := json.RawMessage("{}")
    31  	e := &Event{Type: &name, RawPayload: &body}
    32  	e.Payload()
    33  }
    34  
    35  func TestEmptyEvent_NoPanic(t *testing.T) {
    36  	t.Parallel()
    37  	e := &Event{}
    38  	if _, err := e.ParsePayload(); err == nil {
    39  		t.Error("ParsePayload unexpectedly succeeded on empty event")
    40  	}
    41  
    42  	e = nil
    43  	if _, err := e.ParsePayload(); err == nil {
    44  		t.Error("ParsePayload unexpectedly succeeded on nil event")
    45  	}
    46  }
    47  
    48  func TestEvent_Marshal(t *testing.T) {
    49  	t.Parallel()
    50  	testJSONMarshal(t, &Event{}, "{}")
    51  
    52  	l := make(map[string]interface{})
    53  	l["key"] = "value"
    54  
    55  	jsonMsg, _ := json.Marshal(&l)
    56  
    57  	u := &Event{
    58  		Type:       String("t"),
    59  		Public:     Bool(false),
    60  		RawPayload: (*json.RawMessage)(&jsonMsg),
    61  		Repo: &Repository{
    62  			ID:   Int64(1),
    63  			URL:  String("s"),
    64  			Name: String("n"),
    65  		},
    66  		Actor: &User{
    67  			Login:     String("l"),
    68  			ID:        Int64(1),
    69  			NodeID:    String("n"),
    70  			URL:       String("u"),
    71  			ReposURL:  String("r"),
    72  			EventsURL: String("e"),
    73  			AvatarURL: String("a"),
    74  		},
    75  		Org: &Organization{
    76  			BillingEmail:                         String("be"),
    77  			Blog:                                 String("b"),
    78  			Company:                              String("c"),
    79  			Email:                                String("e"),
    80  			TwitterUsername:                      String("tu"),
    81  			Location:                             String("loc"),
    82  			Name:                                 String("n"),
    83  			Description:                          String("d"),
    84  			IsVerified:                           Bool(true),
    85  			HasOrganizationProjects:              Bool(true),
    86  			HasRepositoryProjects:                Bool(true),
    87  			DefaultRepoPermission:                String("drp"),
    88  			MembersCanCreateRepos:                Bool(true),
    89  			MembersCanCreateInternalRepos:        Bool(true),
    90  			MembersCanCreatePrivateRepos:         Bool(true),
    91  			MembersCanCreatePublicRepos:          Bool(false),
    92  			MembersAllowedRepositoryCreationType: String("marct"),
    93  			MembersCanCreatePages:                Bool(true),
    94  			MembersCanCreatePublicPages:          Bool(false),
    95  			MembersCanCreatePrivatePages:         Bool(true),
    96  		},
    97  		CreatedAt: &Timestamp{referenceTime},
    98  		ID:        String("id"),
    99  	}
   100  
   101  	want := `{
   102  		"type": "t",
   103  		"public": false,
   104  		"payload": {
   105  			"key": "value"
   106  		},
   107  		"repo": {
   108  			"id": 1,
   109  			"name": "n",
   110  			"url": "s"
   111  		},
   112  		"actor": {
   113  			"login": "l",
   114  			"id": 1,
   115  			"node_id": "n",
   116  			"avatar_url": "a",
   117  			"url": "u",
   118  			"events_url": "e",
   119  			"repos_url": "r"
   120  		},
   121  		"org": {
   122  			"name": "n",
   123  			"company": "c",
   124  			"blog": "b",
   125  			"location": "loc",
   126  			"email": "e",
   127  			"twitter_username": "tu",
   128  			"description": "d",
   129  			"billing_email": "be",
   130  			"is_verified": true,
   131  			"has_organization_projects": true,
   132  			"has_repository_projects": true,
   133  			"default_repository_permission": "drp",
   134  			"members_can_create_repositories": true,
   135  			"members_can_create_public_repositories": false,
   136  			"members_can_create_private_repositories": true,
   137  			"members_can_create_internal_repositories": true,
   138  			"members_allowed_repository_creation_type": "marct",
   139  			"members_can_create_pages": true,
   140  			"members_can_create_public_pages": false,
   141  			"members_can_create_private_pages": true
   142  		},
   143  		"created_at": ` + referenceTimeStr + `,
   144  		"id": "id"
   145  	}`
   146  
   147  	testJSONMarshal(t, u, want)
   148  }