1// Copyright 2016-2020 The Libsacloud Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//      http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package sacloud
16
17import (
18	"encoding/json"
19	"testing"
20
21	"github.com/stretchr/testify/assert"
22)
23
24var testWebAccelSiteJSON = `
25{
26      "ID": "123456789012",
27      "Name": "サイト1",
28      "DomainType": "own_domain",
29      "Domain": "cdn1.example.com",
30      "Subdomain": "0f5cty4g.user.webaccel.jp",
31      "ASCIIDomain": "cdn1.example.com",
32      "Origin": "198.51.100.1",
33      "HostHeader": "cdn2.example.com",
34      "Status": "enabled",
35      "CreatedAt": "2015-11-13T02:56:01+09:00",
36      "HasCertificate": true,
37      "HasOldCertificate": true,
38      "GibSentInLastWeek": 80,
39      "CertValidNotBefore": 1457568000000,
40      "CertValidNotAfter": 1526558400000
41}
42`
43
44var testWebAccelCertResponseValid = `
45{
46  "Certificate": {
47    "Current":{
48      "ID": "1",
49      "SiteID": "123456789012",
50      "CertificateChain": "-----BEGIN CERTIFICATE-----・・・・・",
51      "Key": "-----BEGIN RSA PRIVATE KEY-----・・・・・",
52      "CreatedAt": "2015-11-13T02:57:01+09:00",
53      "UpdatedAt": "2015-11-14T02:57:01+09:00"
54    },
55    "Old": [
56      {
57        "ID": "1",
58        "SiteID": "123456789012",
59        "CertificateChain": "-----BEGIN CERTIFICATE-----・・・・・",
60        "CreatedAt": "2015-11-13T02:57:01+09:00",
61        "UpdatedAt": "2015-11-14T02:57:01+09:00"
62      }
63    ]
64  },
65  "Success": true,
66  "is_ok": true
67}
68`
69var testWebAccelCertResponseNotExists = `
70{
71  "Certificate": [],
72  "Success": true,
73  "is_ok": true
74}
75`
76
77func TestMarshalWebAccelSiteJSON(t *testing.T) {
78	var site WebAccelSite
79
80	err := json.Unmarshal([]byte(testWebAccelSiteJSON), &site)
81	assert.NoError(t, err)
82	assert.NotEmpty(t, site.ID)
83	assert.NotEmpty(t, site.Name)
84	assert.NotEmpty(t, site.DomainType)
85	assert.NotEmpty(t, site.Domain)
86	assert.NotEmpty(t, site.Subdomain)
87	assert.NotEmpty(t, site.ASCIIDomain)
88	assert.NotEmpty(t, site.Origin)
89	assert.NotEmpty(t, site.HostHeader)
90	assert.NotEmpty(t, site.Status)
91	assert.NotEmpty(t, site.CreatedAt)
92	assert.True(t, site.HasCertificate)
93	assert.True(t, site.HasOldCertificate)
94	assert.NotEmpty(t, site.GibSentInLastWeek)
95	assert.NotEmpty(t, site.CertValidNotBefore)
96	assert.NotEmpty(t, site.CertValidNotAfter)
97
98}
99
100func TestMarshalWebAccelCertResponseJSON(t *testing.T) {
101	t.Run("Has cert response", func(t *testing.T) {
102		var res WebAccelCertResponse
103		err := json.Unmarshal([]byte(testWebAccelCertResponseValid), &res)
104
105		assert.NoError(t, err)
106		assert.NotNil(t, res.Certificate)
107		assert.NotNil(t, res.Certificate.Current)
108		assert.True(t, len(res.Certificate.Old) > 0)
109	})
110	t.Run("Not exists response", func(t *testing.T) {
111		var res WebAccelCertResponse
112		err := json.Unmarshal([]byte(testWebAccelCertResponseNotExists), &res)
113
114		assert.NoError(t, err)
115		assert.Nil(t, res.Certificate)
116	})
117}
118