Skip to content

Commit 726c0d6

Browse files
committed
test: added unit test for apple callback
1 parent d84f8d0 commit 726c0d6

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [unreleased]
9+
- Adds unit test for Apple callback form post
910

1011
## [0.10.2] - 2023-02-24
1112
- Adds APIs and logic to the dashboard recipe to enable email password based login
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/* Copyright (c) 2021, VRAI Labs and/or its affiliates. All rights reserved.
2+
*
3+
* This software is licensed under the Apache License, Version 2.0 (the
4+
* "License") as published by the Apache Software Foundation.
5+
*
6+
* You may not use this file except in compliance with the License. You may
7+
* obtain a copy of the License at 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, WITHOUT
11+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12+
* License for the specific language governing permissions and limitations
13+
* under the License.
14+
*/
15+
16+
package thirdparty
17+
18+
import (
19+
"io/ioutil"
20+
"net/http"
21+
"net/http/httptest"
22+
"net/url"
23+
"strings"
24+
"testing"
25+
26+
"github.com/stretchr/testify/assert"
27+
"github.com/supertokens/supertokens-golang/recipe/thirdparty/tpmodels"
28+
"github.com/supertokens/supertokens-golang/supertokens"
29+
"github.com/supertokens/supertokens-golang/test/unittesting"
30+
)
31+
32+
func TestAppleRedirectFormPost(t *testing.T) {
33+
configValue := supertokens.TypeInput{
34+
Supertokens: &supertokens.ConnectionInfo{
35+
ConnectionURI: "http://localhost:8080",
36+
},
37+
AppInfo: supertokens.AppInfo{
38+
APIDomain: "api.supertokens.io",
39+
AppName: "SuperTokens",
40+
WebsiteDomain: "supertokens.io",
41+
},
42+
RecipeList: []supertokens.Recipe{
43+
Init(
44+
&tpmodels.TypeInput{
45+
SignInAndUpFeature: tpmodels.TypeInputSignInAndUp{
46+
Providers: []tpmodels.TypeProvider{
47+
Apple(tpmodels.AppleConfig{
48+
ClientID: "4398792-io.supertokens.example",
49+
ClientSecret: tpmodels.AppleClientSecret{
50+
KeyId: "7M48Y4RYDL",
51+
PrivateKey: "-----BEGIN PRIVATE KEY-----\nMIGTAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBHkwdwIBAQQgu8gXs+XYkqXD6Ala9Sf/iJXzhbwcoG5dMh1OonpdJUmgCgYIKoZIzj0DAQehRANCAASfrvlFbFCYqn3I2zeknYXLwtH30JuOKestDbSfZYxZNMqhF/OzdZFTV0zc5u5s3eN+oCWbnvl0hM+9IW0UlkdA\n-----END PRIVATE KEY-----",
52+
TeamId: "YWQCXGJRJL",
53+
},
54+
}),
55+
},
56+
},
57+
},
58+
),
59+
},
60+
}
61+
62+
BeforeEach()
63+
unittesting.StartUpST("localhost", "8080")
64+
defer AfterEach()
65+
err := supertokens.Init(configValue)
66+
67+
if err != nil {
68+
t.Error(err.Error())
69+
}
70+
71+
mux := http.NewServeMux()
72+
testServer := httptest.NewServer(supertokens.Middleware(mux))
73+
defer testServer.Close()
74+
75+
client := &http.Client{}
76+
77+
formData := url.Values{}
78+
formData.Set("state", "afc596274293e1587315c")
79+
formData.Set("code", "c7685e261f98e4b3b94e34b3a69ff9cf4.0.rvxt.eE8rO__6hGoqaX1B7ODPmA")
80+
81+
req, err := http.NewRequest("POST", testServer.URL+"/auth/callback/apple", strings.NewReader(formData.Encode()))
82+
assert.NoError(t, err)
83+
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
84+
85+
resp, err := client.Do(req)
86+
assert.NoError(t, err)
87+
88+
bodyBytes, err := ioutil.ReadAll(resp.Body)
89+
assert.NoError(t, err)
90+
91+
assert.Equal(t, "<html><head><script>window.location.replace(\"https://supertokens.io/auth/callback/apple?state=afc596274293e1587315c&code=c7685e261f98e4b3b94e34b3a69ff9cf4.0.rvxt.eE8rO__6hGoqaX1B7ODPmA\");</script></head></html>", string(bodyBytes))
92+
}

0 commit comments

Comments
 (0)