added OCAccountManager class
[platform/upstream/iotivity.git] / resource / unittests / OCAccountManagerTest.cpp
1 /* ****************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  *
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  ******************************************************************/
20
21 #include <OCPlatform.h>
22 #include <OCApi.h>
23 #include <gtest/gtest.h>
24
25 namespace OCAccountManagerTest
26 {
27     using namespace OC;
28
29     // Callbacks
30     void accountHandler(const HeaderOptions& /*headerOptions*/, const OCRepresentation& /*rep*/,
31             const int /*eCode*/)
32     {
33     }
34
35     // Helper method
36     OCAccountManager::Ptr ConstructAccountManagerObject(std::string host)
37     {
38         auto ret = OCPlatform::constructAccountManagerObject(host, CT_DEFAULT);
39
40         if (!ret)
41         {
42             ADD_FAILURE() << "ConstructAccountManagerObject result was null";
43             throw std::runtime_error("ConstructAccountManagerObject result was null");
44         }
45
46         return ret;
47     }
48
49     // Host Test
50     TEST(HostTest, Host)
51     {
52         std::string host("coap://192.168.1.2:5000");
53         OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
54         EXPECT_TRUE(NULL != accountManager);
55         EXPECT_TRUE(accountManager->host() == host);
56     }
57
58     // ConnectivityType Test
59     TEST(ConnectivityTypeTest, ConnectivityType)
60     {
61         std::string host("coap://192.168.1.2:5000");
62         OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
63         EXPECT_TRUE(NULL != accountManager);
64         EXPECT_TRUE(accountManager->connectivityType() == CT_DEFAULT);
65     }
66
67     // SignUp Test
68     TEST(SignUpTest, DISABLED_SignUpWithoutOptionForValid)
69     {
70         std::string host("coap://192.168.1.2:5000");
71         std::string authProvider("AnyAuthProvider");
72         std::string authCode("AnyAuthCode");
73         OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
74         EXPECT_TRUE(NULL != accountManager);
75         EXPECT_EQ(OC_STACK_OK, accountManager->signUp(authProvider, authCode, &accountHandler));
76     }
77
78     TEST(SignUpTest, DISABLED_SignUpForValid)
79     {
80         std::string host("coap://192.168.1.2:5000");
81         std::string authProvider("AnyAuthProvider");
82         std::string authCode("AnyAuthCode");
83         QueryParamsMap options = {};
84         options.insert(std::pair<std::string, std::string>("AnyOptionKey", "AnyOptionValue"));
85         OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
86         EXPECT_TRUE(NULL != accountManager);
87         EXPECT_EQ(OC_STACK_OK, accountManager->signUp(authProvider, authCode, options,
88                                                       &accountHandler));
89     }
90
91     TEST(SignUpTest, SignUpWithNullCallback)
92     {
93         std::string host("coap://192.168.1.2:5000");
94         std::string authProvider("AnyAuthProvider");
95         std::string authCode("AnyAuthCode");
96         OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
97         EXPECT_TRUE(NULL != accountManager);
98         EXPECT_ANY_THROW(accountManager->signUp(authProvider, authCode, nullptr));
99     }
100
101     // SignIn Test
102     TEST(SignInTest, DISABLED_SignInForValid)
103     {
104         std::string host("coap://192.168.1.2:5000");
105         std::string userId("AnyUserId");
106         std::string accessToken("AnyAccessToken");
107         OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
108         EXPECT_TRUE(NULL != accountManager);
109         EXPECT_EQ(OC_STACK_OK, accountManager->signIn(userId, accessToken, &accountHandler));
110     }
111
112     TEST(SignInTest, SignInWithNullCallback)
113     {
114         std::string host("coap://192.168.1.2:5000");
115         std::string userId("AnyUserId");
116         std::string accessToken("AnyAccessToken");
117         OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
118         EXPECT_TRUE(NULL != accountManager);
119         EXPECT_ANY_THROW(accountManager->signIn(userId, accessToken, nullptr));
120     }
121
122     // SignOut Test
123     TEST(SignOutTest, DISABLED_SignOutForValid)
124     {
125         std::string host("coap://192.168.1.2:5000");
126         std::string userId("AnyUserId");
127         std::string accessToken("AnyAccessToken");
128         OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
129         EXPECT_TRUE(NULL != accountManager);
130         EXPECT_EQ(OC_STACK_OK, accountManager->signOut(userId, accessToken, &accountHandler));
131     }
132
133     TEST(SignOutTest, SignOutWithNullCallback)
134     {
135         std::string host("coap://192.168.1.2:5000");
136         std::string userId("AnyUserId");
137         std::string accessToken("AnyAccessToken");
138         OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
139         EXPECT_TRUE(NULL != accountManager);
140         EXPECT_ANY_THROW(accountManager->signOut(userId, accessToken, nullptr));
141     }
142
143     // RefreshAccessToken Test
144     TEST(RefreshAccessTokenTest, DISABLED_RefreshAccessTokenForValid)
145     {
146         std::string host("coap://192.168.1.2:5000");
147         std::string userId("AnyUserId");
148         std::string refreshToken("AnyRefreshToken");
149         OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
150         EXPECT_TRUE(NULL != accountManager);
151         EXPECT_EQ(OC_STACK_OK, accountManager->refreshAccessToken(userId, refreshToken,
152                                                                   &accountHandler));
153     }
154
155     TEST(RefreshAccessTokenTest, RefreshAccessTokenWithNullCallback)
156     {
157         std::string host("coap://192.168.1.2:5000");
158         std::string userId("AnyUserId");
159         std::string refreshToken("AnyRefreshToken");
160         OCAccountManager::Ptr accountManager = ConstructAccountManagerObject(host);
161         EXPECT_TRUE(NULL != accountManager);
162         EXPECT_ANY_THROW(accountManager->refreshAccessToken(userId, refreshToken, nullptr));
163     }
164 }