• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / gui / 1350829378

27 Jun 2024 01:46PM UTC coverage: 83.494% (-16.5%) from 99.965%
1350829378

Pull #4465

gitlab-ci

mzedel
chore: test fixes

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #4465: MEN-7169 - feat: added multi sorting capabilities to devices view

4506 of 6430 branches covered (70.08%)

81 of 100 new or added lines in 14 files covered. (81.0%)

1661 existing lines in 163 files now uncovered.

8574 of 10269 relevant lines covered (83.49%)

160.6 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.4
/src/js/auth.js
1
// Copyright 2016 Northern.tech AS
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
import Cookies from 'universal-cookie';
15

16
import { TIMEOUTS } from './constants/appConstants';
17

18
const cookies = new Cookies();
184✔
19

20
const emptySession = Object.freeze({ token: '', exiresAt: undefined });
184✔
21

22
let tokenCache = '';
184✔
23

24
export const getSessionInfo = () => {
184✔
25
  let sessionInfo = { ...emptySession };
391✔
26
  try {
391✔
27
    sessionInfo = JSON.parse(window.localStorage.getItem('JWT') ?? '');
391✔
28
  } catch (error) {
29
    // most likely not logged in - nothing to do here
30
  }
31
  if (sessionInfo.expiresAt && new Date(sessionInfo.expiresAt) < new Date()) {
391✔
32
    cleanUp();
7✔
33
    return { ...emptySession };
7✔
34
  }
35
  if (!sessionInfo.token) {
384✔
36
    let jwtTokenFromCookie = cookies.get('JWT', { doNotParse: true }) ?? '';
71✔
37
    if (jwtTokenFromCookie) {
71!
UNCOV
38
      setSessionInfo({ token: jwtTokenFromCookie, undefined });
×
UNCOV
39
      sessionInfo.token = jwtTokenFromCookie;
×
UNCOV
40
      cookies.remove('JWT');
×
UNCOV
41
      cookies.remove('JWT', { path: '/' });
×
42
    }
43
  }
44
  sessionInfo.token = sessionInfo.token || undefined;
384✔
45
  tokenCache = sessionInfo.token;
384✔
46
  return sessionInfo;
384✔
47
};
48

49
export const getToken = () => (tokenCache ? tokenCache : getSessionInfo().token);
2,159✔
50

51
export const setSessionInfo = ({ token, expiresAt }) => {
184✔
52
  tokenCache = token;
6✔
53
  window.localStorage.setItem('JWT', JSON.stringify({ token, expiresAt }));
6✔
54
};
55

56
export const cleanUp = () => {
184✔
57
  tokenCache = '';
18✔
58
  cookies.remove('JWT');
18✔
59
  cookies.remove('JWT', { path: '/' });
18✔
60
  window.localStorage.removeItem('JWT');
18✔
61
  window.localStorage.removeItem('oauth');
18✔
62
};
63

64
export const maxSessionAge = 900;
184✔
65

66
export const updateMaxAge = ({ expiresAt, token }) => {
184✔
67
  const oAuthExpiration = Number(window.localStorage.getItem('oauth'));
3✔
68
  let updateWithOAuth = false;
3✔
69
  if (oAuthExpiration) {
3!
UNCOV
70
    const soon = Date.now() + maxSessionAge * TIMEOUTS.oneSecond;
×
UNCOV
71
    updateWithOAuth = oAuthExpiration <= soon;
×
UNCOV
72
    if (updateWithOAuth) {
×
UNCOV
73
      window.localStorage.removeItem('oauth');
×
74
    }
75
  }
76
  if (token && expiresAt && (!oAuthExpiration || updateWithOAuth)) {
3!
77
    const expiration = new Date();
1✔
78
    expiration.setSeconds(expiration.getSeconds() + maxSessionAge);
1✔
79
    setSessionInfo({ token, expiresAt: expiration.toISOString() });
1✔
80
  }
81
};
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc