1/*
2 * Copyright 2018 - Present Okta, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package tests
18
19import (
20	"net/http"
21	"strconv"
22	"time"
23
24	"github.com/jarcoal/httpmock"
25)
26
27func Mock429Response() *http.Response {
28	loc, _ := time.LoadLocation("UTC")
29	zulu := time.Now().In(loc)
30	header := http.Header{}
31	header.Add("X-Okta-Now", strconv.FormatInt(zulu.Unix(), 10))
32	header.Add("X-Rate-Limit-Reset", strconv.FormatInt(time.Now().Unix()+1, 10))
33	header.Add("X-Okta-Request-id", "a-request-id")
34	header.Add("Content-Type", "application/json")
35	header.Add("Date", zulu.Format("Mon, 02 Jan 2006 15:04:05 Z"))
36
37	return &http.Response{
38		Status:        strconv.Itoa(429),
39		StatusCode:    429,
40		Body:          httpmock.NewRespBodyFromString("{}"),
41		Header:        header,
42		ContentLength: -1,
43	}
44}
45
46func Mock429ResponseNoResetHeader() *http.Response {
47	loc, _ := time.LoadLocation("UTC")
48	zulu := time.Now().In(loc)
49	header := http.Header{}
50	header.Add("X-Okta-Now", strconv.FormatInt(zulu.Unix(), 10))
51	header.Add("X-Okta-Request-id", "a-request-id")
52	header.Add("Content-Type", "application/json")
53	header.Add("Date", zulu.Format("Mon, 02 Jan 2006 15:04:05 Z"))
54
55	return &http.Response{
56		Status:        strconv.Itoa(429),
57		StatusCode:    429,
58		Body:          httpmock.NewRespBodyFromString("{}"),
59		Header:        header,
60		ContentLength: -1,
61	}
62}
63
64func Mock429ResponseNoDateHeader() *http.Response {
65	loc, _ := time.LoadLocation("UTC")
66	zulu := time.Now().In(loc)
67	header := http.Header{}
68	header.Add("X-Okta-Now", strconv.FormatInt(zulu.Unix(), 10))
69	header.Add("X-Rate-Limit-Reset", strconv.FormatInt(time.Now().Unix()+1, 10))
70	header.Add("X-Okta-Request-id", "a-request-id")
71	header.Add("Content-Type", "application/json")
72
73	return &http.Response{
74		Status:        strconv.Itoa(429),
75		StatusCode:    429,
76		Body:          httpmock.NewRespBodyFromString("{}"),
77		Header:        header,
78		ContentLength: -1,
79	}
80}
81
82func Mock429ResponseMultipleHeaders() *http.Response {
83	loc, _ := time.LoadLocation("UTC")
84	zulu := time.Now().In(loc)
85	header := http.Header{}
86	header.Add("X-Okta-Now", strconv.FormatInt(zulu.Unix(), 10))
87	header.Add("X-Rate-Limit-Reset", strconv.FormatInt(time.Now().Unix()+20, 10))
88	header.Add("X-Rate-Limit-Reset", strconv.FormatInt(time.Now().Unix()+10, 10))
89	header.Add("X-Okta-Request-id", "a-request-id")
90	header.Add("Content-Type", "application/json")
91	header.Add("Date", zulu.Format("Mon, 02 Jan 2006 15:04:05 Z"))
92
93	return &http.Response{
94		Status:        strconv.Itoa(429),
95		StatusCode:    429,
96		Body:          httpmock.NewRespBodyFromString("{}"),
97		Header:        header,
98		ContentLength: -1,
99	}
100}
101
102func MockValidResponse() *http.Response {
103	header := http.Header{}
104	header.Add("X-Okta-Request-id", "another-request-id")
105	header.Add("Content-Type", "application/json")
106	header.Add("Date", time.Now().Add(time.Duration(time.Second*10)).Format(time.RFC3339))
107
108	return &http.Response{
109		Status:        strconv.Itoa(200),
110		StatusCode:    200,
111		Body:          httpmock.NewRespBodyFromString("[]"),
112		Header:        header,
113		ContentLength: -1,
114	}
115}
116
117func MockSessionCreateResponse() *http.Response {
118	header := http.Header{}
119	header.Add("X-Okta-Request-id", "another-request-id")
120	header.Add("Content-Type", "application/json")
121	header.Add("Accept", "application/json")
122	header.Add("Date", time.Now().Add(time.Duration(time.Second*10)).Format(time.RFC3339))
123
124	return &http.Response{
125		Status:     strconv.Itoa(200),
126		StatusCode: 200,
127		Body: httpmock.NewRespBodyFromString(`{
128			"id": "101W_juydrDRByB7fUdRyE2JQ",
129			"login": "user@example.com",
130			"userId": "00ubgaSARVOQDIOXMORI",
131			"expiresAt": "2015-08-30T18:41:35.818Z",
132			"status": "ACTIVE",
133			"lastPasswordVerification": "2015-08-30T18:41:35.818Z",
134			"lastFactorVerification": null,
135			"amr": [
136			  "pwd"
137			],
138			"idp": {
139			  "id": "00oi5cpnylv792IcF0g3",
140			  "type": "OKTA"
141			},
142			"mfaActive": false}`),
143		Header:        header,
144		ContentLength: -1,
145	}
146}
147