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

mendersoftware / gui / 897326496

pending completion
897326496

Pull #3752

gitlab-ci

mzedel
chore(e2e): made use of shared timeout & login checking values to remove code duplication

Signed-off-by: Manuel Zedel <manuel.zedel@northern.tech>
Pull Request #3752: chore(e2e-tests): slightly simplified log in test + separated log out test

4395 of 6392 branches covered (68.76%)

8060 of 9780 relevant lines covered (82.41%)

126.17 hits per line

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

86.72
/src/js/selectors/index.js
1
// Copyright 2020 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 { createSelector } from '@reduxjs/toolkit';
15

16
import { mapUserRolesToUiPermissions } from '../actions/userActions';
17
import { PLANS } from '../constants/appConstants';
18
import { DEPLOYMENT_STATES } from '../constants/deploymentConstants';
19
import {
20
  ATTRIBUTE_SCOPES,
21
  DEVICE_ISSUE_OPTIONS,
22
  DEVICE_LIST_MAXIMUM_LENGTH,
23
  DEVICE_ONLINE_CUTOFF,
24
  EXTERNAL_PROVIDER,
25
  UNGROUPED_GROUP
26
} from '../constants/deviceConstants';
27
import { rolesByName, twoFAStates, uiPermissionsById } from '../constants/userConstants';
28
import { attributeDuplicateFilter, duplicateFilter, getDemoDeviceAddress as getDemoDeviceAddressHelper } from '../helpers';
29

30
const getAppDocsVersion = state => state.app.docsVersion;
679✔
31
export const getFeatures = state => state.app.features;
3,617✔
32
const getRolesById = state => state.users.rolesById;
711✔
33
const getOrganization = state => state.organization.organization;
2,126✔
34
const getAcceptedDevices = state => state.devices.byStatus.accepted;
190✔
35
const getDevicesById = state => state.devices.byId;
456✔
36
const getGroupsById = state => state.devices.groups.byId;
190✔
37
const getSearchedDevices = state => state.app.searchState.deviceIds;
429✔
38
const getListedDevices = state => state.devices.deviceList.deviceIds;
190✔
39
const getFilteringAttributes = state => state.devices.filteringAttributes;
190✔
40
const getFilteringAttributesFromConfig = state => state.devices.filteringAttributesConfig.attributes;
190✔
41
const getDeviceLimit = state => state.devices.limit;
190✔
42
const getDevicesList = state => Object.values(state.devices.byId);
190✔
43
const getOnboarding = state => state.onboarding;
718✔
44
const getShowHelptips = state => state.users.showHelptips;
716✔
45
const getGlobalSettings = state => state.users.globalSettings;
707✔
46
const getIssueCountsByType = state => state.monitor.issueCounts.byType;
190✔
47
const getReleasesById = state => state.releases.byId;
190✔
48
const getListedReleases = state => state.releases.releasesList.releaseIds;
190✔
49
const getExternalIntegrations = state => state.organization.externalDeviceIntegrations;
190✔
50
const getDeploymentsById = state => state.deployments.byId;
190✔
51
const getDeploymentsByStatus = state => state.deployments.byStatus;
190✔
52

53
export const getCurrentUser = state => state.users.byId[state.users.currentUser] || {};
764✔
54
export const getUserSettings = state => state.users.userSettings;
1,556✔
55

56
export const getHas2FA = createSelector(
190✔
57
  [getCurrentUser],
58
  currentUser => currentUser.hasOwnProperty('tfa_status') && currentUser.tfa_status === twoFAStates.enabled
2!
59
);
60

61
export const getDemoDeviceAddress = createSelector([getDevicesList, getOnboarding], (devices, { approach, demoArtifactPort }) => {
190✔
62
  const demoDeviceAddress = `http://${getDemoDeviceAddressHelper(devices, approach)}`;
2✔
63
  return demoArtifactPort ? `${demoDeviceAddress}:${demoArtifactPort}` : demoDeviceAddress;
2!
64
});
65

66
const listItemMapper = (byId, ids, { defaultObject = {}, cutOffSize = DEVICE_LIST_MAXIMUM_LENGTH }) => {
190✔
67
  return ids.slice(0, cutOffSize).reduce((accu, id) => {
456✔
68
    if (id && byId[id]) {
198!
69
      accu.push({ ...defaultObject, ...byId[id] });
198✔
70
    }
71
    return accu;
198✔
72
  }, []);
73
};
74

75
const listTypeDeviceIdMap = {
190✔
76
  deviceList: getListedDevices,
77
  search: getSearchedDevices
78
};
79
const getDeviceMappingDefaults = () => ({ defaultObject: { auth_sets: [] }, cutOffSize: DEVICE_LIST_MAXIMUM_LENGTH });
431✔
80
export const getMappedDevicesList = createSelector(
190✔
81
  [getDevicesById, (state, listType) => listTypeDeviceIdMap[listType](state), getDeviceMappingDefaults],
431✔
82
  listItemMapper
83
);
84

85
const defaultIdAttribute = Object.freeze({ attribute: 'id', scope: ATTRIBUTE_SCOPES.identity });
190✔
86
export const getIdAttribute = createSelector([getGlobalSettings], ({ id_attribute = { ...defaultIdAttribute } }) => id_attribute);
190✔
87

88
export const getLimitMaxed = createSelector([getAcceptedDevices, getDeviceLimit], ({ total: acceptedDevices = 0 }, deviceLimit) =>
190!
89
  Boolean(deviceLimit && deviceLimit <= acceptedDevices)
4✔
90
);
91

92
export const getFilterAttributes = createSelector(
190✔
93
  [getGlobalSettings, getFilteringAttributes],
94
  ({ previousFilters }, { identityAttributes, inventoryAttributes, systemAttributes, tagAttributes }) => {
95
    const deviceNameAttribute = { key: 'name', value: 'Name', scope: ATTRIBUTE_SCOPES.tags, category: ATTRIBUTE_SCOPES.tags, priority: 1 };
3✔
96
    const deviceIdAttribute = { key: 'id', value: 'Device ID', scope: ATTRIBUTE_SCOPES.identity, category: ATTRIBUTE_SCOPES.identity, priority: 1 };
3✔
97
    const checkInAttribute = { key: 'updated_ts', value: 'Last check-in', scope: ATTRIBUTE_SCOPES.system, category: ATTRIBUTE_SCOPES.system, priority: 4 };
3✔
98
    const firstRequestAttribute = { key: 'created_ts', value: 'First request', scope: ATTRIBUTE_SCOPES.system, category: ATTRIBUTE_SCOPES.system, priority: 4 };
3✔
99
    const attributes = [
3✔
100
      ...previousFilters.map(item => ({
×
101
        ...item,
102
        value: deviceIdAttribute.key === item.key ? deviceIdAttribute.value : item.key,
×
103
        category: 'recently used',
104
        priority: 0
105
      })),
106
      deviceNameAttribute,
107
      deviceIdAttribute,
108
      ...identityAttributes.map(item => ({ key: item, value: item, scope: ATTRIBUTE_SCOPES.identity, category: ATTRIBUTE_SCOPES.identity, priority: 1 })),
3✔
109
      ...inventoryAttributes.map(item => ({ key: item, value: item, scope: ATTRIBUTE_SCOPES.inventory, category: ATTRIBUTE_SCOPES.inventory, priority: 2 })),
3✔
110
      ...tagAttributes.map(item => ({ key: item, value: item, scope: ATTRIBUTE_SCOPES.tags, category: ATTRIBUTE_SCOPES.tags, priority: 3 })),
×
111
      checkInAttribute,
112
      firstRequestAttribute,
113
      ...systemAttributes.map(item => ({ key: item, value: item, scope: ATTRIBUTE_SCOPES.system, category: ATTRIBUTE_SCOPES.system, priority: 4 }))
×
114
    ];
115
    return attributeDuplicateFilter(attributes, 'key');
3✔
116
  }
117
);
118

119
export const getGroups = createSelector([getGroupsById], groupsById => {
190✔
120
  const groupNames = Object.keys(groupsById).sort();
10✔
121
  const groupedGroups = Object.entries(groupsById)
10✔
122
    .sort((a, b) => a[0].localeCompare(b[0]))
28✔
123
    .reduce(
124
      (accu, [groupname, group]) => {
125
        const name = groupname === UNGROUPED_GROUP.id ? UNGROUPED_GROUP.name : groupname;
26✔
126
        const groupItem = { ...group, groupId: name, name: groupname };
26✔
127
        if (group.filters.length > 0) {
26✔
128
          if (groupname !== UNGROUPED_GROUP.id) {
16✔
129
            accu.dynamic.push(groupItem);
10✔
130
          } else {
131
            accu.ungrouped.push(groupItem);
6✔
132
          }
133
        } else {
134
          accu.static.push(groupItem);
10✔
135
        }
136
        return accu;
26✔
137
      },
138
      { dynamic: [], static: [], ungrouped: [] }
139
    );
140
  return { groupNames, ...groupedGroups };
10✔
141
});
142

143
export const getDeviceTwinIntegrations = createSelector([getExternalIntegrations], integrations =>
190✔
144
  integrations.filter(integration => integration.id && EXTERNAL_PROVIDER[integration.provider]?.deviceTwin)
4✔
145
);
146

147
export const getOfflineThresholdSettings = createSelector([getGlobalSettings], ({ offlineThreshold }) => ({
190✔
148
  interval: offlineThreshold?.interval || DEVICE_ONLINE_CUTOFF.interval,
24✔
149
  intervalUnit: offlineThreshold?.intervalUnit || DEVICE_ONLINE_CUTOFF.intervalName
24✔
150
}));
151

152
export const getOnboardingState = createSelector([getOnboarding, getShowHelptips], ({ complete, progress, showTips }, showHelptips) => ({
190✔
153
  complete,
154
  progress,
155
  showHelptips,
156
  showTips
157
}));
158

159
export const getDocsVersion = createSelector([getAppDocsVersion, getFeatures], (appDocsVersion, { isHosted }) => {
190✔
160
  // if hosted, use latest docs version
161
  const docsVersion = appDocsVersion ? `${appDocsVersion}/` : 'development/';
40!
162
  return isHosted ? '' : docsVersion;
40✔
163
});
164

165
export const getIsEnterprise = createSelector(
190✔
166
  [getOrganization, getFeatures],
167
  ({ plan = PLANS.os.value }, { isEnterprise, isHosted }) => isEnterprise || (isHosted && plan === PLANS.enterprise.value)
60✔
168
);
169

170
export const getAttributesList = createSelector(
190✔
171
  [getFilteringAttributes, getFilteringAttributesFromConfig],
172
  ({ identityAttributes = [], inventoryAttributes = [] }, { identity = [], inventory = [] }) =>
16!
173
    [...identityAttributes, ...inventoryAttributes, ...identity, ...inventory].filter(duplicateFilter)
8✔
174
);
175

176
export const getUserRoles = createSelector(
190✔
177
  [getCurrentUser, getRolesById, getIsEnterprise, getFeatures, getOrganization],
178
  (currentUser, rolesById, isEnterprise, { isHosted, hasMultitenancy }, { plan = PLANS.os.value }) => {
290✔
179
    const isAdmin = currentUser.roles?.length
336✔
180
      ? currentUser.roles.some(role => role === rolesByName.admin)
41✔
181
      : !(hasMultitenancy || isEnterprise || (isHosted && plan !== PLANS.os.value));
317!
182
    const uiPermissions = isAdmin
336✔
183
      ? mapUserRolesToUiPermissions([rolesByName.admin], rolesById)
184
      : mapUserRolesToUiPermissions(currentUser.roles || [], rolesById);
568✔
185
    return { isAdmin, uiPermissions };
336✔
186
  }
187
);
188

189
const hasPermission = (thing, permission) => Object.values(thing).some(permissions => permissions.includes(permission));
2,517✔
190

191
export const getUserCapabilities = createSelector([getUserRoles], ({ uiPermissions }) => {
190✔
192
  const canManageReleases = hasPermission(uiPermissions.releases, uiPermissionsById.manage.value);
333✔
193
  const canReadReleases = canManageReleases || hasPermission(uiPermissions.releases, uiPermissionsById.read.value);
333✔
194
  const canUploadReleases = canManageReleases || hasPermission(uiPermissions.releases, uiPermissionsById.upload.value);
333✔
195

196
  const canAuditlog = uiPermissions.auditlog.includes(uiPermissionsById.read.value);
333✔
197

198
  const canReadUsers = uiPermissions.userManagement.includes(uiPermissionsById.read.value);
333✔
199
  const canManageUsers = uiPermissions.userManagement.includes(uiPermissionsById.manage.value);
333✔
200

201
  const canReadDevices = hasPermission(uiPermissions.groups, uiPermissionsById.read.value);
333✔
202
  const canWriteDevices = Object.values(uiPermissions.groups).some(
333✔
203
    groupPermissions => groupPermissions.includes(uiPermissionsById.read.value) && groupPermissions.length > 1
49✔
204
  );
205
  const canTroubleshoot = hasPermission(uiPermissions.groups, uiPermissionsById.connect.value);
333✔
206
  const canManageDevices = hasPermission(uiPermissions.groups, uiPermissionsById.manage.value);
333✔
207
  const canConfigure = hasPermission(uiPermissions.groups, uiPermissionsById.configure.value);
333✔
208

209
  const canDeploy = uiPermissions.deployments.includes(uiPermissionsById.deploy.value) || hasPermission(uiPermissions.groups, uiPermissionsById.deploy.value);
333✔
210
  const canReadDeployments = uiPermissions.deployments.includes(uiPermissionsById.read.value);
333✔
211

212
  return {
333✔
213
    canAuditlog,
214
    canConfigure,
215
    canDeploy,
216
    canManageDevices,
217
    canManageReleases,
218
    canManageUsers,
219
    canReadDeployments,
220
    canReadDevices,
221
    canReadReleases,
222
    canReadUsers,
223
    canTroubleshoot,
224
    canUploadReleases,
225
    canWriteDevices
226
  };
227
});
228

229
export const getTenantCapabilities = createSelector(
190✔
230
  [getFeatures, getOrganization, getIsEnterprise],
231
  (
232
    {
233
      hasAuditlogs: isAuditlogEnabled,
234
      hasDeviceConfig: isDeviceConfigEnabled,
235
      hasDeviceConnect: isDeviceConnectEnabled,
236
      hasMonitor: isMonitorEnabled,
237
      isHosted
238
    },
239
    { addons = [], plan },
8✔
240
    isEnterprise
241
  ) => {
242
    const canDelta = isEnterprise || plan === PLANS.professional.value;
40✔
243
    const hasAuditlogs = isAuditlogEnabled && (!isHosted || isEnterprise || plan === PLANS.professional.value);
40!
244
    const hasDeviceConfig = isDeviceConfigEnabled && (!isHosted || addons.some(addon => addon.name === 'configure' && Boolean(addon.enabled)));
40!
245
    const hasDeviceConnect = isDeviceConnectEnabled && (!isHosted || addons.some(addon => addon.name === 'troubleshoot' && Boolean(addon.enabled)));
40!
246
    const hasMonitor = isMonitorEnabled && (!isHosted || addons.some(addon => addon.name === 'monitor' && Boolean(addon.enabled)));
40!
247
    return {
40✔
248
      canDelta,
249
      canRetry: canDelta,
250
      canSchedule: canDelta,
251
      hasAuditlogs,
252
      hasDeviceConfig,
253
      hasDeviceConnect,
254
      hasFullFiltering: canDelta,
255
      hasMonitor,
256
      isEnterprise
257
    };
258
  }
259
);
260

261
export const getAvailableIssueOptionsByType = createSelector(
190✔
262
  [getFeatures, getTenantCapabilities, getIssueCountsByType],
263
  ({ hasReporting }, { hasFullFiltering, hasMonitor }, issueCounts) =>
264
    Object.values(DEVICE_ISSUE_OPTIONS).reduce((accu, { isCategory, key, needsFullFiltering, needsMonitor, needsReporting, title }) => {
15✔
265
      if (isCategory || (needsReporting && !hasReporting) || (needsFullFiltering && !hasFullFiltering) || (needsMonitor && !hasMonitor)) {
90!
266
        return accu;
90✔
267
      }
268
      accu[key] = { count: issueCounts[key].filtered, key, title };
×
269
      return accu;
×
270
    }, {})
271
);
272

273
export const getDeviceTypes = createSelector([getAcceptedDevices, getDevicesById], ({ deviceIds = [] }, devicesById) =>
190!
274
  Object.keys(
1✔
275
    deviceIds.slice(0, 200).reduce((accu, item) => {
276
      const { device_type: deviceTypes = [] } = devicesById[item] ? devicesById[item].attributes : {};
2!
277
      accu = deviceTypes.reduce((deviceTypeAccu, deviceType) => {
2✔
278
        if (deviceType.length > 1) {
2!
279
          deviceTypeAccu[deviceType] = deviceTypeAccu[deviceType] ? deviceTypeAccu[deviceType] + 1 : 1;
2!
280
        }
281
        return deviceTypeAccu;
2✔
282
      }, accu);
283
      return accu;
2✔
284
    }, {})
285
  )
286
);
287

288
const getReleaseMappingDefaults = () => ({});
190✔
289
export const getReleasesList = createSelector([getReleasesById, getListedReleases, getReleaseMappingDefaults], listItemMapper);
190✔
290

291
const relevantDeploymentStates = [DEPLOYMENT_STATES.pending, DEPLOYMENT_STATES.inprogress, DEPLOYMENT_STATES.finished];
190✔
292
export const DEPLOYMENT_CUTOFF = 3;
190✔
293
export const getRecentDeployments = createSelector([getDeploymentsById, getDeploymentsByStatus], (deploymentsById, deploymentsByStatus) =>
190✔
294
  Object.entries(deploymentsByStatus).reduce(
30✔
295
    (accu, [state, byStatus]) => {
296
      if (!relevantDeploymentStates.includes(state) || !byStatus.deploymentIds.length) {
120✔
297
        return accu;
48✔
298
      }
299
      accu[state] = byStatus.deploymentIds
72✔
300
        .reduce((accu, id) => {
301
          if (deploymentsById[id]) {
129✔
302
            accu.push(deploymentsById[id]);
123✔
303
          }
304
          return accu;
129✔
305
        }, [])
306
        .slice(0, DEPLOYMENT_CUTOFF);
307
      accu.total += byStatus.total;
72✔
308
      return accu;
72✔
309
    },
310
    { total: 0 }
311
  )
312
);
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