OsAccount.test.js 11.3 KB
Newer Older
J
jiyong_sd 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

1
15829070344 已提交
16
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect, done } from '@ohos/hypium'
J
jiyong_sd 已提交
17 18

import account from '@ohos.account.distributedAccount'
1
15829070344 已提交
19 20 21 22 23 24 25 26 27 28
const LOGININFO = {
    name: 'ZhangSan',
    id: '12345',
    event: "Ohos.account.event.LOGIN"
}
const LOGOUTINFO = {
    name: 'ZhangSan',
    id: '12345',
    event: "Ohos.account.event.LOGOUT"
}
J
jiyong_sd 已提交
29 30 31 32 33 34 35 36 37 38 39 40
export default function AccountTest() {
    describe('AccountTest', function () {
        beforeEach(function () {
        })
        afterEach(function () {
        })

        /**
        * @tc.number     ActsDistributedAccountDeviceId_0100
        * @tc.name       Test query the distribruted id by callback.
        * @tc.desc       Test distributedAccount.queryOsAccountDistributedInfo API functionality by callback.
        */
1
15829070344 已提交
41 42 43
        it('ActsDistributedAccountDeviceId_0100', 0, async function(done){
            console.log("====>test query distribtued id start");
            const distributedId = '5994471ABB01112AFCC18159F6CC74B4F511B99806DA59B3CAF5A9C173CACFC5'; //'12345'sha256的值
J
jiyong_sd 已提交
44
            const accountAbility = account.getDistributedAccountAbility();
1
15829070344 已提交
45 46
            accountAbility.updateOsAccountDistributedInfo(LOGININFO, (err)=>{
                console.log("====>update distributedInfo err:" + JSON.stringify(err));
J
jiyong_sd 已提交
47
                accountAbility.queryOsAccountDistributedInfo((err, distributedInfo)=>{
1
15829070344 已提交
48 49
                    console.log("====>query distributedInfo err:" + JSON.stringify(err));
                    console.log("====>query distributedInfo:" + JSON.stringify(distributedInfo));
J
jiyong_sd 已提交
50 51
                    expect(distributedInfo.name).assertEqual('ZhangSan');
                    expect(distributedInfo.id).assertEqual(distributedId);
1
15829070344 已提交
52 53 54 55 56 57 58 59
                    console.debug('success')
                    accountAbility.updateOsAccountDistributedInfo(LOGOUTINFO, (err)=>{
                        console.debug('====>ActsDistributedAccountDeviceId_0100 logout_result:'+ JSON.stringify(err))
                        expect(err).assertEqual(undefined)
                        console.log("====>test query distribtued id end");
                        done();
                    })

J
jiyong_sd 已提交
60 61 62 63 64 65 66 67 68
                })
            })
        })

        /**
        * @tc.number     SUB_Account_distributedAccount_JS_API_0100
        * @tc.name       Test distributedAccount.getDistributedAccountAbility.
        * @tc.desc      Test distributedAccount.getDistributedAccountAbility API functionality.
        */
1
15829070344 已提交
69
        it('account_getDistributedAccountAbility_test', 0, async function (done) {
J
jiyong_sd 已提交
70 71
            var ret = false;
            const accountAbility = account.getDistributedAccountAbility()
1
15829070344 已提交
72
            console.debug("====>account_getDistributedAccountAbility_test result:" + JSON.stringify(accountAbility))
J
jiyong_sd 已提交
73 74 75 76
            if(accountAbility !== null){
                ret = true;
            }
            expect(ret).assertTrue()
1
15829070344 已提交
77
            done();
J
jiyong_sd 已提交
78 79 80 81 82 83 84
        })

        /**
        * @tc.number     SUB_Account_distributedAccount_JS_API_0200
        * @tc.name       Test distributedAccount.queryOsAccountDistributedInfo.
        * @tc.desc       Test distributedAccount.queryOsAccountDistributedInfo API functionality.
        */
1
15829070344 已提交
85 86
        it('account_queryOsAccountDistributedInfo_test001', 0, async function (done) {
            let accountAbility = account.getDistributedAccountAbility()
J
jiyong_sd 已提交
87
            accountAbility.queryOsAccountDistributedInfo().then(function (data) {
1
15829070344 已提交
88 89 90 91
                console.debug('====>account_queryOsAccountDistributedInfo_test001 data:' + JSON.stringify(data))
                expect(data.name).assertEqual('ohosAnonymousName')
                expect(data.id).assertEqual('ohosAnonymousUid')
                done();
J
jiyong_sd 已提交
92 93 94 95 96 97 98 99
            });
        })

        /**
        * @tc.number     SUB_Account_distributedAccount_JS_API_0300
        * @tc.name       Test distributedAccount.queryOsAccountDistributedInfo by callback.
        * @tc.desc       Test distributedAccount.queryOsAccountDistributedInfo API functionality by callback.
        */
1
15829070344 已提交
100 101 102 103 104 105 106 107 108
        it('account_queryOsAccountDistributedInfo_test002', 0, async function (done) {
            let accountAbility = account.getDistributedAccountAbility()
            accountAbility.queryOsAccountDistributedInfo(function (err, data) {
                console.debug('====>account_queryOsAccountDistributedInfo_test002 err:' + JSON.stringify(err))
                console.debug('====>account_queryOsAccountDistributedInfo_test002 data:' + JSON.stringify(data))
                expect(err).assertEqual(undefined)
                expect(data.name).assertEqual('ohosAnonymousName')
                expect(data.id).assertEqual('ohosAnonymousUid')
                done();
J
jiyong_sd 已提交
109 110 111 112 113 114 115 116
            });
        })

        /**
        * @tc.number     SUB_Account_distributedAccount_JS_API_0400
        * @tc.name       Test distributedAccount.updateOsAccountDistributedInfo.
        * @tc.desc       Test distributedAccount.updateOsAccountDistributedInfo API functionality.
        */
1
15829070344 已提交
117
        it('account_updateOsAccountDistributedInfo_test001', 0, async function (done) {
J
jiyong_sd 已提交
118
            const accountAbility = account.getDistributedAccountAbility()
1
15829070344 已提交
119
            let data = null
J
jiyong_sd 已提交
120 121 122 123 124 125
            let obj = {
                id: '12345',
                name: 'ZhangSan',
                event: 'Ohos.account.event.LOGIN',
                scalableData:data
            };
1
15829070344 已提交
126
            accountAbility.updateOsAccountDistributedInfo(obj).then(function () {
J
jiyong_sd 已提交
127
                accountAbility.queryOsAccountDistributedInfo(function (data) {
1
15829070344 已提交
128
                    console.debug('====>account_updateOsAccountDistributedInfo_test001 data:' + JSON.stringify(data))
J
jiyong_sd 已提交
129 130 131 132 133 134 135
                    expect(data.name).assertEqual('ZhangSan')
                    expect(data.id).assertEqual('12345')
                    let obj = {
                        id: '12345',
                        name: 'ZhangSan',
                        event: 'Ohos.account.event.LOGOUT'
                    };
1
15829070344 已提交
136 137
                    accountAbility.updateOsAccountDistributedInfo(obj).then(function (err) {
                        expect(err).assertEqual(undefined)
J
jiyong_sd 已提交
138 139 140 141
                    });
                });
            });
            accountAbility.queryOsAccountDistributedInfo(function (data) {
1
15829070344 已提交
142 143 144

                expect(data).assertEqual(undefined)
                done();
J
jiyong_sd 已提交
145 146 147 148
            })
        })

        /**
1
15829070344 已提交
149
        * @tc.number     SUB_Account_distributedAccount_JS_API_0200
J
jiyong_sd 已提交
150 151 152
        * @tc.name       Test distributedAccount.updateOsAccountDistributedInfo by callback.
        * @tc.desc       Test distributedAccount.updateOsAccountDistributedInfo API functionality by callback.
        */
1
15829070344 已提交
153 154
        it('account_updateOsAccountDistributedInfo_test002', 0,  async function (done)  {
            var accountAbility = account.getDistributedAccountAbility()
J
jiyong_sd 已提交
155 156 157 158
            let obj = {
                id: '12345',
                name: 'ZhangSan',
                event: 'Ohos.account.event.LOGIN',
1
15829070344 已提交
159
                scalableData:{}
J
jiyong_sd 已提交
160
            };
1
15829070344 已提交
161 162 163
            accountAbility.updateOsAccountDistributedInfo(obj, function () {
                accountAbility.queryOsAccountDistributedInfo(function (err, data) {
                    console.debug("====>account_updateOsAccountDistributedInfo_test002 data:" + JSON.stringify(data))
J
jiyong_sd 已提交
164
                    expect(data.name).assertEqual('ZhangSan')
1
15829070344 已提交
165
                    expect(data.id).assertEqual('5994471ABB01112AFCC18159F6CC74B4F511B99806DA59B3CAF5A9C173CACFC5')
J
jiyong_sd 已提交
166 167 168 169 170
                    let obj = {
                        id: '12345',
                        name: 'ZhangSan',
                        event: 'Ohos.account.event.LOGOUT'
                    };
1
15829070344 已提交
171 172 173
                    accountAbility.updateOsAccountDistributedInfo(obj).then(function (data) {
                        expect(data).assertEqual(undefined)
                        done();
J
jiyong_sd 已提交
174 175 176 177
                    });
                });
            });
        })
1
15829070344 已提交
178

J
jiyong_sd 已提交
179 180 181 182 183
        /**
        * @tc.number     SUB_Account_distributedAccount_JS_API_0300
        * @tc.name       Test distributedAccount.updateOsAccountDistributedInfo by callback.
        * @tc.desc       Test distributedAccount.updateOsAccountDistributedInfo API functionality by callback.
        */
1
15829070344 已提交
184
        it('account_updateOsAccountDistributedInfo_test003', 0, async function (done) {
J
jiyong_sd 已提交
185
            const accountAbility = account.getDistributedAccountAbility()
1
15829070344 已提交
186
            let data = null
J
jiyong_sd 已提交
187 188 189 190 191 192 193
            let obj = {
                id: '12345',
                name: 'ZhangSan',
                event: 'Ohos.account.event.TOKEN_INVALID',
                scalableData:data
            };
            accountAbility.updateOsAccountDistributedInfo(obj, function (result) {
1
15829070344 已提交
194
                console.debug("====>account_updateOsAccountDistributedInfo_test003 update_err:" + JSON.stringify(result))
J
jiyong_sd 已提交
195
                accountAbility.queryOsAccountDistributedInfo(obj).then(function (data) {
1
15829070344 已提交
196
                    console.debug("====>account_updateOsAccountDistributedInfo_test003 data:" + JSON.stringify(data))
1
15829070344 已提交
197
                    expect(data.name).assertEqual("ohosAnonymousName")
1
15829070344 已提交
198
                    done();
J
jiyong_sd 已提交
199 200 201
                })
            });
        })
1
15829070344 已提交
202

J
jiyong_sd 已提交
203 204 205 206 207
        /**
        * @tc.number     SUB_Account_distributedAccount_JS_API_0500
        * @tc.name       Test distributedAccount.updateOsAccountDistributedInfo by callback.
        * @tc.desc       Test distributedAccount.updateOsAccountDistributedInfo API functionality by callback.
        */
1
15829070344 已提交
208
        it('account_updateOsAccountDistributedInfo_test004', 0, async function (done) {
J
jiyong_sd 已提交
209 210 211 212 213
            const accountAbility = account.getDistributedAccountAbility()
            let obj = {
                id: '12345',
                name: 'ZhangSan',
                event: 'Ohos.account.event.LOGIN',
1
15829070344 已提交
214
                scalableData:{}
J
jiyong_sd 已提交
215
            };
1
15829070344 已提交
216 217 218 219 220
            accountAbility.updateOsAccountDistributedInfo(obj, function (err) {
                console.debug("====>account_updateOsAccountDistributedInfo_test004 update_err:" + JSON.stringify(err))
                accountAbility.queryOsAccountDistributedInfo(function (err, data) {
                    console.debug("====>account_updateOsAccountDistributedInfo_test004 query_err:" + JSON.stringify(err))
                    console.debug("====>account_updateOsAccountDistributedInfo_test004 query_data:" + JSON.stringify(data))
J
jiyong_sd 已提交
221
                    expect(data.name).assertEqual('ZhangSan')
1
15829070344 已提交
222
                    expect(data.id).assertEqual('5994471ABB01112AFCC18159F6CC74B4F511B99806DA59B3CAF5A9C173CACFC5')
J
jiyong_sd 已提交
223 224 225 226 227
                    let obj = {
                        id: '12345',
                        name: 'ZhangSan',
                        event: 'Ohos.account.event.LOGOFF'
                    };
1
15829070344 已提交
228
                    accountAbility.updateOsAccountDistributedInfo(obj).then(function (err) {
1
15829070344 已提交
229
                        expect(err).assertEqual(undefined)
1
15829070344 已提交
230
                        done();
J
jiyong_sd 已提交
231 232 233 234 235 236
                    });
                });
            });
        })
    })
}